Can angularjs ng-click process events during the capturing phase?

前端 未结 1 1200
面向向阳花
面向向阳花 2020-12-15 21:44

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

相关标签:
1条回答
  • 2020-12-15 21:58

    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

    0 讨论(0)
提交回复
热议问题