Can the process track the ng-click process during the capture phase?

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

+13
source share
1 answer

Let's see the ng-click source code at ngEventDirs.js # L50

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

So the answer is No, that is not possible .

If you really need it, you can write a special directive for this. For example, modify the ng-click code slightly:

 .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 as follows:

 <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 example: http://plnkr.co/edit/SVPv0fCNRQX4JXHeL47X?p=preview

+16
source

Source: https://habr.com/ru/post/1271463/


All Articles