简体   繁体   English

使用element.bind获取单击的嵌套元素的值

[英]Get value of clicked nested element with element.bind

I am using the following code in my directive and it always seems like the click is not able to keep track of my $location.$$path very well. 我在指令中使用以下代码,似乎单击似乎总是无法很好地跟踪我的$ location。$$ path。 This plug-in I am using is also using the <li></li> as a clickable item with a path which may be causing the issue. 我正在使用的该插件还将<li></li>用作可单击项,其路径可能会引起问题。 So my question would be if there a way I can skit the out elements and just focus down on the <a href="#myValue> which is a child of that <li> ? 所以我的问题是,是否有一种方法可以略过元素并仅关注<a href="#myValue>这是该<li>的子元素?

.directive('treeClick', function ($location) {
    return {
      restrict: 'A',
      link: function (scope, element, attrs) {
          element.bind("mousedown", function () {
            console.log($location.$$path);
          })
        }
    }
  })

<li class="list-group-item node-tree node-selected" data-nodeid="2" style="color:#000;background-color:#eaeaea;"><span class="indent"></span><span class="indent"></span><span class="icon glyphicon"></span><span class="icon node-icon"></span><a href="#myValue" style="color:inherit;">Example1</a></li>

I'm thinking that for what you're trying to do, you're using the wrong event. 我在想,对于您想要做的事情,您使用了错误的事件。 What if you tried mouseup instead of mousedown (you might also need to $timeout your processing if the update to the url is asynchronous, but I'm thinking it's not)? 如果您尝试使用mouseup而不是mousedown怎么办(如果url更新是异步的,则可能还需要$timeout处理$timeout ,但我认为不是这样)?

Couple more things.. Angular exposes an event for when the location changes named $locationChangeSuccess 多做几件事。当位置更改时,Angular公开一个事件,名为$locationChangeSuccess

And.. $$path is an internal property, why not use the documented $location.path() 而且。 $$path是一个内部属性,为什么不使用记录的$location.path()

https://docs.angularjs.org/api/ng/service/ $location https://docs.angularjs.org/api/ng/service/ $ location

event.target.nodeName gave me what the element was: "A" for anchor "LI" for list item. event.target.nodeName给出了元素的含义:列表项的锚点“ LI”为“ A”。 I then extracted the value of the "A" by using event.target.nodeName.getAttribute("href"); 然后,我使用event.target.nodeName.getAttribute("href");提取“ A”的值event.target.nodeName.getAttribute("href"); so that I do not get the host name in the console.log() 这样我就不会在console.log()中获得主机名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM