【发布时间】:2016-06-26 20:29:51
【问题描述】:
我有一个带有控制器的父指令,它在 $scope 上设置一个方法。
然后,在子指令中,我尝试通过& 要求该范围方法。
但是,子指令中的ng-click 不会与父作用域函数一起触发。我做错了吗?
父指令(定义方法的地方)
app.directive('ParentDirective', [
'$http',
function($http) {
return {
restrict: 'AE',
template: '' +
'<div child-directive ' +
'list="list" ' +
'/>' +
'',
controller: function ($scope, $http) {
$scope.setSelected = function (selector) {
console.log('hit!', selector);
}
$scope.list = [];
},
link: function postLink(scope, element, attrs) {}
};
}
]);
子指令(尝试在 ng-click 上调用父方法,但不起作用)
app.directive('childDirective', [
'$window',
function($window) {
return {
restrict: 'AE',
scope: {
setSelected: '&',
list: '=',
},
template: '' +
'<ul>' +
'<li ng-repeat="person in list" ng-click="setSelected(person)">{{person.uri}}</li>' +
'</ul>' +
'',
link: function postLink(scope, element, attrs) {
}
};
}
]);
我是否将 $scope.setSelected() 错误地从父指令传递到子指令?
编辑:所以我更改了父模板以分配函数,如下所示: 模板:'' + '' + '',
this 现在将触发函数,但参数不会从子指令传递。如何让子指令将参数传递给父指令函数?
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope isolate-scope