在捕获阶段可以angularjs NG单击处理事件? [英] Can angularjs ng-click process events during the capturing phase?

查看:682
本文介绍了在捕获阶段可以angularjs NG单击处理事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能有在捕获阶段,而不是冒泡阶段angularjs NG单击处理事件?我想为了从每个父元素的汇总数据,从父开始,以被点击的元素结束。

Is it possible to have angularjs ng-click process events during the capturing phase instead of bubbling phase? I want to aggregate data from each of the parent elements in order starting from the parent and ending with the element that was clicked on.

推荐答案

让我们来看看 NG-点击来源$ C ​​$ c。在<一个href=\"https://github.com/angular/angular.js/blob/master/src/ng/directive/ngEventDirs.js#L50\">ngEventDirs.js#L50

Lets see the source code of ng-click at ngEventDirs.js#L50

正如你所看到的 NG-点击,并使用所有其他事件的指令。对()

As you can see the ng-click and all other event directives using .on().

所以,答案是否,这是不可能

如果你真的需要它,你可以写一个自定义的指令。例如,修改 NG-点击的code A位:

If you really need it, you could write a custom directive for that. For example, modify the code of ng-click a bit:

.directive('captureClick', function($parse) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      var fn = $parse(attrs.captureClick);
      return function(scope, element) {
        element[0].addEventListener('click', function(event) {
          scope.$apply(function() {
            fn(scope, {
              $event: event
            });
          });
        }, true);
      };
    }
  }
});

和使用这样的:

<div title="A" ng-click="onBubbled($event)" capture-click="onCaptured($event)">
  <div title="B" ng-click="onBubbled($event)" capture-click="onCaptured($event)">
    <div title="C" ng-click="onBubbled($event)" capture-click="onCaptured($event)">
      Yo!
    </div>
  </div>
</div>

示例Plunker: http://plnkr.co/edit/SVPv0fCNRQX4JXHeL47X?p=$p$pview

这篇关于在捕获阶段可以angularjs NG单击处理事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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