Drame
Drame

Reputation: 23

Angularjs ng-click doesn't fire inside directive

I created a directive that adds a line of table when one clicks on the button to add to the level of the directive -- more precisely the button of the last column. I want that when one clicks on this button that a method is in my controller and then call

    myapp.directive("newCandidat", function() {
        return {
            restrict : "E",
            template : "<tr>"+
                          "<td><input  class='form-control' value='' disabled='disabled'></td>"+
                          "<td><input  class='form-control' value='' disabled='disabled'></td>"+
                          "<td><input  class='form-control' value=''></td>"+
                          "<td><button ng-click='method()'>click</button></td>"+
                        "</tr>",
            replace: true,
            transclude:true,
            terminal: true,
            scope:{method:'&'},
            link: function(scope, element, attrs) {
                console.log(element);
              }

        };
    });

    myapp.controller("Controller",function($scope,$compile){
        $scope.addCand=function(){
            angular.element($("#candList")).append($compile("<new-candidat method='clickMe()'><new-candidat>")($scope));
        }
        $scope.clickMe=function(){
           console.log("Click me"); 
        }
    });

Upvotes: 1

Views: 168

Answers (1)

Frank Modica
Frank Modica

Reputation: 10516

This was a bit surprising, but it works without terminal: true. It seems that terminal:true stops compiling a directive's template, even if it's the only directive applied to an element. Check out this plnkr to see it working.

From the docs:

terminal

If set to true then the current priority will be the last set of directives which will execute (any directives at the current priority will still execute as the order of execution on same priority is undefined). Note that expressions and other directives used in the directive's template will also be excluded from execution.

This was also reported here.

Do you need terminal: true? If so, your directive may have to compile its contents. Check out this answer.

Upvotes: 1

Related Questions