Reputation: 157
In jQuery, I can get the element that was clicked in the DOM using the following
$(document).click(function(event) {
var element = $(event.target);
console.log(element);
....
}
How would I do the same with Angular? I've searched for it online but all of the solutions were related to elements that had an ng-click
attribute like links and buttons. I'd like to listen to the click event for the entire DOM and then get the element that was clicked(p, li, a, span etc.) Is there anyway I can achieve this using Angular only or would I have to use jQuery?
Upvotes: 2
Views: 1449
Reputation: 5468
Place the ng-click on the body
<body ng-click="go($event)">
In your controller:
$scope.go = function($event){
alert($event.currentTarget );
};
Upvotes: 2
Reputation: 6200
From the docs https://docs.angularjs.org/api/ng/directive/ngClick and https://docs.angularjs.org/guide/expression#-event-:
Event object is available as $event
html
<div ng-click="functionName()"></div>
js
$scope.functionName = function($event){}
Upvotes: 2