I am learning Angular2, after working with Angular1 for several years. I am creating a component of a credit card form, with the main goal to learn a few key concepts in Angular2. The component should handle all formatting and return a Stripe token through a callback. I realized that I can handle callbacks in two ways.
Using the @Output Option
In my component, I define the output variable and use it as in this example:
export class CreditCardForm{ .... @Output () callback = new EventEmitter(); .... doCallback(){ this.callback.emit({data: 123}); } }
Using the @Input Variable
However, I could just pass the callback method ( creditCardCallback used in the parent template) to the input variable, for example:
export class CreditCardForm{ .... @Input () callback; .... doCallback(){ this.callback({data: 123}); } }
Question
Why do I want to use @Output over @Input ? What can I do using @Output variables @Output ? As far as I can see, this just adds overhead due to the use of the EventEmitter class.
angular typescript
Anton Gildebrand
source share