我試圖實現嵌套指令。內部指令是一個按鈕,它在ng-click中調用一個函數。要調用的函數的名稱在模型中定義。嵌套指令中的變量函數名稱
首先,這裏是plunker鏈接。 PLUNKER
問題是該按鈕不知道要調用的函數。 有趣的是,如果我使用ng-include和變量中正確命名的變量的外部指令的模板,它就像一個魅力。
某些代碼段爲您提供:
的index.html:
DIRECTIVES
<outer-directive functions="functions" datas="datas"></outer-directive>
<p>{{clicked}}</p>
NG-Include :
<div ng-include src="'outer-template.html'"></div>
模板外指令
<div ng-repeat="d in datas">
Hi, {{d}}
<inner-directive
ng-repeat="funct in functions"
text="funct.text"
method="this[funct.method](d)">
</inner-directive>
</div>
控制器
app.controller('MainCtrl', function($scope) {
$scope.datas = [0, 1];
$scope.functions = [{
method: 'functOne',
text: 'Funct One'
}, {
method: 'functTwo',
text: 'Funct Two'
}];
$scope.clicked = 'Nothing happend';
$scope.functOne = function(data) {
$scope.clicked = data + ' pressed Funct 1';
}
$scope.functTwo = function(data) {
$scope.clicked = data + ' pressed Funct 2';
}
});
外指令JS
app.directive('outerDirective', function() {
return {
restrict: 'E',
scope: {
functions: '=',
datas: '='
},
templateUrl: 'outer-template.html'
}
});
內指令JS
app.directive('innerDirective', function() {
return {
restrict: 'E',
scope: {
method: '&',
text: '=',
datas: '='
},
template: '<button ng-click="method(datas)"> {{text}}</button>'
}
});
所以,我決定去與$發射和$(見[這個問題](https://stackoverflow.com/questions/14502006/working- with-scope-emit-and-scope-on)) –