【问题标题】:Angularjs ng-click doesn't fire inside directiveAngularjs ng-click 不会在指令内部触发
【发布时间】:2017-11-01 14:35:52
【问题描述】:

我创建了一个指令,当单击按钮以添加到指令级别时添加一行表格 - 更准确地说是最后一列的按钮。我希望当单击此按钮时,我的控制器中有一个方法,然后调用

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

【问题讨论】:

    标签: angularjs directive angularjs-ng-click


    【解决方案1】:

    这有点令人惊讶,但没有terminal: true 也能正常工作。 terminal:true 似乎停止编译指令的模板,即使它是应用于元素的唯一指令。看看这个plnkr 看看它的工作原理。

    来自docs

    终端

    如果设置为 true 则当前优先级将是最后一组 将执行的指令(当前优先级的任何指令 仍将执行,因为相同优先级的执行顺序是 不明确的)。请注意,在 指令的模板也将被排除在执行之外。

    这也被报告了here

    你需要terminal: true吗?如果是这样,您的指令可能必须编译其内容。查看this answer

    【讨论】:

    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2013-10-21
    • 1970-01-01
    • 2015-12-06
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多