我有一个实现选择框的指令。
现在,当选择框打开并且我单击其外部的某个位置(文档中的任何其他位置)时,我需要它折叠。
这个 JQuery 代码在我的指令中工作,但我想以“有角度的方式”执行它:
$(document).bind('click', function (e) {
var $clicked = e.target;
if (!$clicked.parents().hasClass("myClass")) {
scope.colapse();
}
});
我尝试将 $document 服务注入到我的指令中,但没有成功。
我相信,最真实的 Angular 方式是使用angular.element
代替jQuery
并访问window.document
通过$document
服务:
(function() {
angular.module('myApp').directive('myDocumentClick',
['$window', '$document', '$log',
function($window, $document, $log) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
$document.bind('click', function(event) {
$log.info(event);
if (angular.element(event.target).hasClass('myClass')) {
$window.alert('Foo!');
}
})
}
};
}
]);
})();
笨蛋链接 http://plnkr.co/edit/TBdfMLp9mIBhZR9E4wXo?p=preview
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)