1

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"); 
        }
    });
Frank Modica
  • 10,238
  • 3
  • 23
  • 39
Drame
  • 23
  • 1
  • 6

1 Answers1

1

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.

Frank Modica
  • 10,238
  • 3
  • 23
  • 39
  • can you tell me in witch situation we use terminal? – Drame Jun 02 '17 at 14:07
  • There may be situations where you need to control the timing of what gets compiled. Check out these links: https://stackoverflow.com/questions/18969610/why-use-terminal-true-instead-of-removing-lower-priority-directives https://stackoverflow.com/questions/15266840/how-to-understand-the-terminal-of-directive – Frank Modica Jun 02 '17 at 14:19