2017-05-31 70 views
1

我创建了一个指令,当点击按钮添加到指令级别时,添加一行表格 - 更确切地说,是最后一列的按钮。我想,当一个用户点击此按钮,一个方法是在我的控制器,然后调用Angularjs ng-click不会触发指令

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"); 
     } 
    }); 

回答

1

这是一个有点出人意料,但它的作品没有terminal: true。看起来terminal:true停止编译指令的模板,即使它是应用于元素的唯一指令。看看这plnkr看到它的工作。

docs

终端

如果设置为真,则当前的优先级将是最后集 指令将执行(任何指示在当前优先权 仍将执行如未定义的相同优先级的执行顺序为 )。请注意, 指令模板中使用的表达式和其他指令也将被排除在执行之外。

这也报告了here

你需要terminal: true?如果是这样,您的指示可能需要编译其内容。退房this answer

+0

你能告诉我在巫婆的情况下,我们使用终端吗? – Drame

+0

可能有些情况需要控制编译时间。看看这些链接: https://stackoverflow.com/questions/18969610/why-use-terminal-true-instead-of-removing-lower-priority-directives https://stackoverflow.com/questions/ 15266840 /如何理解的最终端的指令性 –