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

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

问题描述

在捕获阶段而不是冒泡阶段是否可以有 angularjs ng-click 过程事件?我想按照从父元素开始到被点击的元素结束的顺序聚合来自每个父元素的数据.

解决方案

让我们在 ng-click 的源代码.js/blob/master/src/ng/directive/ngEventDirs.js#L50">ngEventDirs.js#L50

如您所见,ng-click 和所有其他使用 .on() 的事件指令.

所以,答案是不,不可能.

如果你真的需要它,你可以为此编写一个自定义指令.比如稍微修改一下ng-click的代码:

.directive('captureClick', function($parse) {返回 {限制:'A',编译:函数(元素,属性){var fn = $parse(attrs.captureClick);返回函数(范围,元素){element[0].addEventListener('click', function(event) {范围.$应用(函数(){fn(范围,{$event: 事件});});}, 真的);};}}});

并像这样使用它:

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

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

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.

解决方案

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

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

So, the answer is No, it is not possible.

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);
      };
    }
  }
});

and use it like this:

<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>

Example Plunker: http://plnkr.co/edit/SVPv0fCNRQX4JXHeL47X?p=preview

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆