我有3个指令,具有隔离范围和共享范围,我希望在最外层的最内层指令之间传递一个函数 . 外部和中部具有隔离范围,中部与内部共用范围 . 有什么建议?
传递我的控制器的功能,如下所示 .
<outer on-edit="helloWorld" ng-model="model" ng-repeat="items in items.objects" ></outer>
在我的控制器中:
$scope.helloWorld = function(){
alert('Hello world');
}
我的指示:
angular.module('myApp')
.directive('outer', function () {
return {
restrict: 'E',
replace: true,
scope: {
item: "=ngModel",
onEdit: '&'
},
template: '<div><middle on-edit='onEdit'></middle></div>',
controller : function($scope){
$scope.edit = function(){
$scope.onEdit()();
}
}
};
})
.directive('middle', function () {
return {
restrict: 'E',
replace: true,
scope: {
item : '=ngModel',
onEdit : '&'
},
templateUrl: '<div><inner on-edit='onEdit'></inner></div>'
};
})
.directive('inner', function () {
return {
restrict: 'E',
template: '<div><a ng-click='edit()'>Edit</a></div>'
};
})
这不起作用,任何想法?
谢谢
1 回答
这看起来很糟糕,但在中间指令的模板中,您使用的是内部指令,如下所示:
如果你看一下,内部指令没有范围,所以on-edit属性在那里没有意义 .
如果要使用中间指令中存在的任何方法,可以直接在内部指令中使用,因为共享范围 . 将内部指令视为在其他html文件中编写的html的一部分,该文件将在运行时替换 . 因此,传递给中间指令的任何内容都会隐式传递给内部 .