Angularjs:如何在自定义指令中捕获“show event”



我想在我的客户指令中捕获"显示事件",例如ng-show .

如何编写这样的事件侦听器?要收听的事件的正确名称是什么?

<button ng-click="showMyDirective = !showMyDirective"></button>
<div my-directive="" ng-show="showMyDirective"></div>

更新:

下面是一个示例指令,其中包含onclick事件的两种侦听器。两者都有效。同样的方法不适用于show/onshow事件。

module.directive( 'myDirective' , function() {
    return { function (scope , element , attrs) {
        element.bind('click', function(){
            console.log('element.bind click');
        });
        var div = element[0];
        div.onclick = function(e){
            console.log('div.onclick');
        };
    }; // return
}); // myDirective

更改ng-click以调用函数:

<button ng-click="toggleMyDirective()"></button>

在控制器中,将"事件侦听器"代码放入函数中:

$scope.toggleMyDirective = function() {
    $scope.showMyDirective = !$scope.showMyDirective;
    ... do other stuff here ...
}

编辑:让您的指令使用控制器和服务:

http://joelhooks.com/blog/2014/02/11/lets-make-full-ass-angularjs-directives/

最新更新