Consider this code (from here):
Parent component:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
template: `<app-child (valueChange)='displayCounter($event)'></app-child>`
})
export class AppComponent implements OnInit {
ngOnInit() {
}
displayCounter(count) {
console.log(count);
}
}
Child component:
import { Component, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'app-child',
template: `<button class='btn btn-primary' (click)="valueChanged()">Click me</button> `
})
export class AppChildComponent {
@Output() valueChange = new EventEmitter();
Counter = 0;
valueChanged() { // You can give any function name
this.counter = this.counter + 1;
this.valueChange.emit(this.counter);
}
}
As you can see in Parent component we use displayCounter($event)
to receive data emitted from child component.
My question is why do we have to use $event
keyword for this?
If I try to change displayCounter($event)
to displayCounter(count)
it is not going to work.
I can however change the name of the method from displayCounter
to say myAwesomeDisplayCounter
.
Usually functions in javascript do not have set parameter names, so I am confused as to why this is the case here in Angular.
Can somebody explain? Or link an article explaining this restriction?