it's the event :)
normal Javascript
<div></div>
div.addEventListener('click', function($event) {
console.log($event);
})
All the javascript native events, like keyup or click, they're all asynchronous events and when they happen, you can listen to them ( add a listener in Javascript, or a HostListener or event binding in Angular) and then in your function which you are passing to be called when the event is happening, you can catch that Event, which is basically a Javascript object which is holding information about the source of the event and all the other things related to the event.
The same thing can be done in Angular:
@HostListener('click', ['$event']) myFunction(theEvent) {
console.log("$event is ", theEvent); //
}
$event is an optional parameter and if you're not interested, you're not forced to add it to your parameters.
As I said $event holds all the information about the actual event, like the name of it, the source of it, if it was a keyup event, the pressed key and all the other things, that means you can get all those extracted from the Event:
@HostListener('click', ['$event.target.id']) myFunction(buttonId) {
console.log("buttonId ",buttonId); //
}
or you can
@HostListener('click', ['$event']) myFunction(event) {
console.log("buttonId ",event.target.id); //
}
The other syntax in Angular is :
<button (click)="myFunction($event)">
Or in normal javascript you can say :
document.querySelector('button').addEventListener('click',($event)=>{
console.log('the event is',$event);
})
The HostListener can also be written in the @Component decorator if you don't like to use the @HostListener decorator to be used ( although why wouldn't you?)
@Component({
selector:"my-component",
host:{
'(click)':'myFunction($event)'
}
})