I have a directive in which I'm passing an array of data via scope. However, when I click on a button with ng-click, the function doesn't work. This is my directive:
angular.module('appModule').directive('myDirective', myDirective);
function myDirective() {
return {
restrict: 'A',
scope: {
myDirective: '=',
},
link: function( scope ) {
scope.enable = function() {
console.log( 'test' );
}
}
}
}
And this is my html:
<ul my-directive="vm.myData">
<li ng-repeat="data in vm.myData track by data.id" ng-click="enable( data.id )">{{data.name}}</li>
</ul>
When I click on the li element, nothing happens. If I remove the scope definition on the directive, then I can call the function as expected.
By looking around, I saw that by using the template inside the directive may solve the problem. But I can't use the template inside the directive because it may change depending on the location, but the expected final behavior will be the same in multiple views.
Also, I need to access the whole array inside the directive for other purposes.
How can I solve this issue?
my-directive, I'm just using it also to pass the data, instead of creating another attribute. But if, for example, I do it like this:my-directive data-array="vm.myData"the result is the same. I edited the question to make it clear =Denableyour directive's scope won't know of that