Is it possible to capture the $event parameter on a click event and also pass in some extra parameter data to a component method?
Example template:
<div *ngFor="let item of data" (click)="onItemClick($event, item)"> {{ item.Name }} </div>
Example component:
onItemClick(event, item) { // do something }
For passing the parameters, we will wrap all the parameters inside the curly brackets (this will combine them as a single object) and pass it to the emit method. To receive the parameters in the parent component, we will make a similar type of object and update its value with that of the received object.
The $event object often contains information the method needs, such as a user's name or an image URL. The target event determines the shape of the $event object. If the target event is a native DOM element event, then $event is a DOM event object, with properties such as target and target.
Introduction to AngularJs onclick The ng-click directive can be used with multiple HTML elements such as button, input, select, checkbox, etc. This provides the developer ability to define custom behavior whenever an element is clicked.
Events are handled in Angular using the following special syntax. Bind the target event name within parentheses on the left of an equal sign, and event handler method or statement on the right. Above, (click) binds the button click event and onShow() statement calls the onShow() method of a component.
Yes it would work fine. But keep the order of parameters same.
<div *ngFor="let item of data" (click)="onItemClick($event, item)">
In this case the second element is your passed element.
onItemClick(event, item) { console.log("Checking passed item: ",item); }
Yes It will work fine. Put console.log inside the function and check.
onItemClick(event, item) { console.log("Event: ",event,"Item: ",item); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With