angular.js - 如何在指令里使用ng-click
阿神
阿神 2017-05-15 17:12:06
0
2
543
    return {
        restrict: 'E',
        replace: true,
        scope: {
            cancelFunc: '&'
        },
        template: '<section class="part-load">'
        + '<p class="part-text">正在加载</p>'
        + '<p class="part-close border-left" ng-click="cancelFunc"></p>'
        + '</section>',
        link: function (scope, elem, attrs) {

        }
    }
}]);

如上,定义了一个指令partload,期望传入属性值cancelFunc,绑定ng-click事件,html结构:

<partload cancel-func="stop()"></partload>

控制器中定义了一个stop方法:

$scope.stop = function () {
            alert(1)
}

然而并不会触发,为啥呢?如何解决呢?

阿神
阿神

闭关修行中......

reply all(2)
黄舟

Thanks for the invitation

The address of the online example: https://plnkr.co/edit/LBb4dN7...
The only difference from young-click="cancelFunc()"

迷茫

Thanks for the invitation

I also made an online example: https://embed.plnkr.co/SirYJd...

Try this

return {
        restrict: 'E',
        replace: true,
        scope: {
            cancelFunc: '&'
        },
        template: '<section class="part-load">'
        + '<p class="part-text">正在加载</p>'
        + '<p class="part-close border-left" ng-click="_cancelFunc()"></p>'
        + '</section>',
        link: function (scope, elem, attrs) {
            scope._cancelFunc = function(){
                // 这里可以写一些指令内部逻辑
                scope.cancelFunc({id: 1}); // { id : 1 } 传参
            }
        }
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template