Created
October 7, 2020 09:53
-
-
Save antonpegov/58d57058af94545990ab72606ff22a05 to your computer and use it in GitHub Desktop.
Listen for changes in Reactive Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| 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