I am looking for an argument about why use @Output
for events is better than passing a function @Input
in Angular 2+.
Usage @Input
:
Parent template:
<my-component [customEventFunction]=myFunction></my-component>
Inside parent-component.ts:
myFunction = () => {
console.log("Hello world")
}
Inside my-component.ts
@Input() customEventFunction: Function;
someFunctionThatTriggersTheEvent() {
this.customEventFunction();
}
Usage @Output
:
Parent template:
<my-component (onCustomEvent)=myFunction()></my-component>
Inside parent-component.ts:
myFunction() {
console.log("Hello world")
}
Inside my-component.ts
@Output() onCustomEvent: EventEmitter<any> = new EventEmitter<any>();
someFunctionThatTriggersTheEvent() {
this.onCustomEvent.emit();
}
Both achieve the same goal, but I think the method is @Output
more typical of what I saw in other Angular packages. It can be argued that with Input you can check if a function exists if the event should be fired only conditionally.
Thoughts?
source
share