Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  110] [ 2]  / answers: 1 / hits: 155361  / 11 Years ago, mon, january 13, 2014, 12:00:00

I know that I can get access to the click event from ng-click if I pass in the $event object like so:



<button ng-click=myFunction($event)>Give me the $event</button>

<script>
function myFunction (event) {
typeof event !== undefined // true
}
</script>


It's a little bit annoying having to pass $event explicitly every time. Is it possible to set ng-click to somehow pass it to the function by default?


More From » angularjs

 Answers
4

Take a peek at the ng-click directive source:



...
compile: function($element, attr) {
var fn = $parse(attr[directiveName]);
return function(scope, element, attr) {
element.on(lowercase(name), function(event) {
scope.$apply(function() {
fn(scope, {$event:event});
});
});
};
}


It shows how the event object is being passed on to the ng-click expression, using $event as a name of the parameter. This is done by the $parse service, which doesn't allow for the parameters to bleed into the target scope, which means the answer is no, you can't access the $event object any other way but through the callback parameter.


[#73193] Sunday, January 12, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmiejudahm

Total Points: 319
Total Questions: 98
Total Answers: 117

Location: Venezuela
Member since Sat, Apr 24, 2021
3 Years ago
;