7. Listen to child component events
In this section we will listen to child component events emitted to the parent.
1. Add event emitter to component
The EventEmitter
in Angular is used to emit event from child components to parent components and can pass what ever object, array primitive, null or anything you would like. Event emitters are made by using Angular's@Output
decorators. We will address the other side of the equation about passing in data to the component with property binding later in the course using the opposite with @Input
decorators.
Add event emitter to component
Emit add attendee event when the form is submitted by the
submit
method.
2. Add event listener to add-event-component selector
Add the event listener to the add-event-component selector. The $event is an angular specific variable which is a generic name for what ever you emitted from the child component.
3. Add method to call when event is emitted
Note we recreate the array every time versus using an array .push
method which would mutate the array.
Add a
addAttendee
method to the component.Declare a
attendees
property on the class and initialise it to an empty array.
StackBlitz Link
Last updated