0

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?

7
  • sorry how you call your directive? ...cause my-directive looks like the name oof the isoltaed scope..not the name of the directive Commented Jan 30, 2017 at 12:39
  • @federicoscamuzzi I call it using 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 =D Commented Jan 30, 2017 at 12:42
  • Your directive has its own scope, so if you call enable your directive's scope won't know of that Commented Jan 30, 2017 at 12:44
  • @devqon but how can I make it work? Commented Jan 30, 2017 at 12:49
  • you mixed the directive name and parameter name . Commented Jan 30, 2017 at 12:49

2 Answers 2

2

since you declared a scope in the directive you isolated the scope, you have to put an html for the directive with the ng-click

http://plnkr.co/edit/8zWou1E9v29PdJFTRMvq?p=preview

  <ul mydirective myparam="myData" >
      <li ng-repeat="data in myData track by data.id" ng-click="enable( data.id )">{{data.name}}</li>
  </ul>

this ng-click will call the function defined in parent controller not in mydirective

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

angular.module('appModule').directive('myDirective', myDirective);
    function myDirective() {
        return {
            restrict: 'A',
            scope: {
                myModel: '=',
            },
            link: function( scope ) {
                scope.enable = function() {
                    console.log( 'test' );
                }
            }
        }
    }

    <ul my-directive my-model="vm.myData">
        <li ng-repeat="data in vm.myData track by data.id" ng-click="enable( data.id )">{{data.name}}</li>
    </ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.