I'm trying to implement nested directives. The inner directive is a button which calls a function in ng-click. The name of the function to be called is defined in the model.
To start off with, here is the plunker link. PLUNKER
The problem is that the button does not know the function to be called. Funnily enough, if I use a template for the outer directive using ng-include and variables rightly named in the scope, it works like a charm.
Some Code Snippets for you:
index.html:
DIRECTIVES
<outer-directive functions="functions" datas="datas"></outer-directive>
<p>{{clicked}}</p>
NG-Include :
<div ng-include src="'outer-template.html'"></div>
Template for outer directive
<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>
Controller
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';
}
});
Outer Directive JS
app.directive('outerDirective', function() {
return {
restrict: 'E',
scope: {
functions: '=',
datas: '='
},
templateUrl: 'outer-template.html'
}
});
Inner Directive JS
app.directive('innerDirective', function() {
return {
restrict: 'E',
scope: {
method: '&',
text: '=',
datas: '='
},
template: '<button ng-click="method(datas)"> {{text}}</button>'
}
});