formGroup: FormGroup;
customMessage: string;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.formGroup = this.formBuilder.group({
fullName: '',
emailAddress: ''
});
this.trackChanges();
}
In the ngOnInit lifecycle hook, we set up our form and then call a method called trackChanges. Here's what trackChanges does:
trackChanges(): void {
this.formGroup.valueChanges.subscribe(data => {
this.customMessage =
`Welcome ${data.fullName}`;
});
}
You can also monitor changes on individual form controls instead of the entire form group:
trackChanges(): void {
this.formGroup.get('fullName').valueChanges.subscribe(name => {
this.customMessage = `I go by ${name}.`;
});
}