从 ng-click 获取原始元素 [英] get original element from ng-click
本文介绍了从 ng-click 获取原始元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的视图中有一个项目列表,并附有 ng-click
:
I have a list of items in my view with ng-click
attached to them:
<ul id="team-filters">
<li ng-click="foo($event, team)" ng-repeat="team in teams">
<img src="{{team.logoSmall}}" alt="{{team.name}}" title="{{team.name}}">
</li>
</ul>
我在指令中的 foo
函数中处理点击事件,将 $event
作为对被点击对象的引用传递,但我得到对 img
标签的引用,而不是 li
标签.然后我必须做这样的事情来获得 li
:
I'm handling the click events in the foo
function in my directive, passing $event
as a reference to the object that's been clicked, but I'm getting a reference to the img
tag, rather than the li
tag. I then have to do stuff like this to get the li
:
$scope.foo = function($event, team) {
var el = (function(){
if ($event.target.nodeName === 'IMG') {
return angular.element($event.target).parent(); // get li
} else {
return angular.element($event.target); // is li
}
})();
是否有一种简单的方法可以获取对 ng-click
绑定到的元素的引用,而无需在我的指令中执行 DOM 操作?
Is there a simple way to get the reference to the element that ng-click
is bound to, without doing DOM operations in my directive?
推荐答案
你需要 $event.currentTarget
而不是 $event.target
.
这篇关于从 ng-click 获取原始元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文