Last active
February 6, 2018 20:29
-
-
Save guillefd/d946eddafcfcf11592b8b2e3676e6b07 to your computer and use it in GitHub Desktop.
Angular, form value change observer
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
import { Component } from '@angular/core'; | |
import { FormBuilder, FormGroup } from '@angular/forms'; | |
@Component({ | |
selector: 'form', | |
templateUrl: ` | |
<form [formGroup]="form"> | |
<input type="text" formControlName="name"> | |
</form> | |
`, | |
styleUrls: ['./form.component.scss'] | |
}) | |
export class FormComponent { | |
form: FormGroup; | |
construct(private fb:FormBuilder){ | |
this.formChangeObserver(); | |
} | |
private formChangeObserver() { | |
this.form.valueChanges | |
.subscribe(data => { | |
console.log('form changed ...'); | |
}); | |
} | |
private initForm() { | |
this.form = this.fb.group({ | |
name: ['',[]] | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment