Mat*_*nis 4 javascript angularjs
如何在Angular中正确访问最接近的元素,如下面的jQuery代码,而不使用jQuery?
link: function (scope, element, attrs) {
function handler($event) {
if(!($event.target).closest(element).length) {
scope.$apply(function () {
$parse(attrs.clickOutside)(scope);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
我最后只是检查元素是否包含$event.target.这里的关键是你必须访问element[0]而不是简单地element:
link: function(scope, element, attrs) {
function handler($event) {
if (!element[0].contains($event.target)) {
scope.$apply(function() {
$parse(attrs.clickOutside)(scope);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4215 次 |
| 最近记录: |