首页 > angular 如何监听文档的点击事件

angular 如何监听文档的点击事件

不是在html用ng-click的方式绑定事件的方式
有像jq的$document.on('click', fn)的处理方式?


这种操作应该通过指令完成,类似这样:

angular.module('demo', [])
.directive('dClick', ['$document', function($document) {
    return {
        restrict: 'A',
        scope: {},
        link: function($scope, element, attrs){
            $document.on('click', function(){
                console.log('clicking....');
            });
        }
    };
}]);

然后把这个指令用在body上就好了:

<html>
    <head>
    </head>
    <body d-click>
    </body>
</html>

参考文档:creating a direcitve that manipulates the dom

【热门文章】
【热门文章】