我得到了这个错误:TypeError:无法读取Scope.$ Scope. dispatchaction .
我想不出是什么问题,你能帮帮我吗?谢谢。——模板——
$templateCache.put("template/dropdown-user.html",
'<ul class="dropdown-menu dropdown-user">'+
'<li ng-repeat-start="item in items">'+
"<a ng-click="click(item,$event)" ng-href="{{item.link}}"><i class="fa fa-{{item.icon}} fa-fw"></i> {{item.label}}</a>"+
'</li>'+
"<li ng-repeat-end class="divider" ng-if="item.isDivider"></li>"+
'</ul>'
);
——指令
.directive('navbarMenuUserItem',[function(){
return {
restrict: 'E',
templateUrl: 'template/dropdown-user.html',
replace: true,
scope:{
items: '=',
click: '&itemClick'
}
}
}])
(HTML)
<navbar-menu-user-item item-click="dispatchAction(obj,event)" items="usermenu"></navbar-menu-user-item>
控制器*
app.controller('dashboardCtrl',
['$scope','$sanitize','$log','$location','dashboardService','AuthService',
function($scope, $sanitize, $log, $location, dashboard,AuthService){
$scope.messages = dashboard.getMessages();
$scope.usermenu = dashboard.getUserMenu();
$scope.sidebarmenu = dashboard.getSidebar();
$scope.logout = function(){
AuthService.logout();
$location.path('/');
};
$scope.dispatchAction = function(obj,event){
event.preventDefault();
console.log(obj);
};
}
]);
可汗的建议是正确的。您必须将您的项目和事件绑定到表达式item-click
应在其中求值的作用域。
scope:{
items: '=',
click: '=itemClick'
}
和你的item-click
属性在HTML:
item-click="dispatchAction"
通过这种方式,你可以将dispatchAction
函数直接绑定到指令作用域中的一个变量。