问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/25296808/can-angularjs-ng-click-process-events-during-the-capturing-phase