从NG-点击获取原始元素 [英] get original element from ng-click

查看:121
本文介绍了从NG-点击获取原始元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图中的项目与 NG-点击连接到他们的列表:

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>

我管我的指令中的函数的单击事件,传递 $事件作为参考对正在被点击,但我得到的 IMG 标签的参考,而不是标记对象。然后,我必须做这样的东西来获取

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-点击被绑定到的元素,没有在我的指示做的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-点击获取原始元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆