Skip to content

Instantly share code, notes, and snippets.

@antonpegov
Created October 7, 2020 09:53
Show Gist options
  • Save antonpegov/58d57058af94545990ab72606ff22a05 to your computer and use it in GitHub Desktop.
Save antonpegov/58d57058af94545990ab72606ff22a05 to your computer and use it in GitHub Desktop.
Listen for changes in Reactive Forms
...
myForm: FormGroup;
formattedMessage: string;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.myForm = this.formBuilder.group({
name: '',
email: '',
message: ''
});
this.onChanges();
}
...
onChanges(): void {
this.myForm.valueChanges.subscribe(val => {
this.formattedMessage =
`Hello,
My name is ${val.name} and my email is ${val.email}.
I would like to tell you that ${val.message}.`;
});
}
------- OR -------
onChanges(): void {
this.myForm.get('name').valueChanges.subscribe(val => {
this.formattedMessage = `My name is ${val}.`;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment