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
import { Component, EventEmitter } from '@angular/core';
import { Output } from '@angular/core';
import { Attendee } from '../../../models';
import { FormGroup } from '@angular/forms';
import { FormControl } from '@angular/forms';
import { Validators } from '@angular/forms';
@Component({
selector: 'app-add-attendee',
templateUrl: './add-attendee.component.html',
styleUrls: ['./add-attendee.component.scss']
})
export class AddAttendeeComponent {
@Output()
addAttendee = new EventEmitter<Attendee>();
addAttendeeForm = new FormGroup({
name: new FormControl('', [Validators.required])
});
submit() {
const attendee = {
name: this.addAttendeeForm.value.name,
attending: true,
guests: 0
};
this.addAttendee.emit(attendee);
}
}2. Add event listener to add-event-component selector
3. Add method to call when event is emitted
StackBlitz Link
Last updated