javascript - angular 如何监听文档的点击事件
本文介绍了javascript - 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
这篇关于javascript - angular 如何监听文档的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文