4

I am new to AngularJS and exploring event handlers. I am going over an existing code base and had no idea why $event is being passed. This is what the html looks like

<p><a ng-click="packBtnClick($event)" href="#" title="">[[btnAction]]</a></p> 

And in the controller,

$scope.packBtnClick = function($e){ $e.preventDefault(); if($scope.packAvailable){ addPackIntoCart(); } else{ //some other code. }; 

The only purpose of passing in an event here is to preventDefault behavior.
My question is - is it really necessary to pass in the $event?

0

1 Answer 1

3

It is necessary if you want to call preventDefault and unnecessary otherwise. If you do not need to call anything exposed by $event, you can safely remove it since not passing it in will not prevent the event from occurring.

You can find the $event documentation here. It is simply a wrapper for the jQuery event object when jQuery is present, or something similar when using jqLite. You can look at the event object for a full list of potentially exposed properties and functions. jqLite is not guaranteed to expose all of the same properties and functions, but it should be very close in nearly all scenarios.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. how would i know what does $event expose?
@ShrutiKapoor you're welcome. I've updated my answer with the $event documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.