Created
May 22, 2024 20:25
-
-
Save rakia/d39e5c07d011235e691246a53e01601c to your computer and use it in GitHub Desktop.
Angular: Unified Control State Change Events
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 { FormBuilder, FormGroup, Validators } from '@angular/forms'; | |
| export class ProfileFormComponent { | |
| private fb = inject(FormBuilder); | |
| profileForm: FormGroup; | |
| ngOnInit() { | |
| this.profileForm = this.fb.group({ | |
| firstName: ['', [Validators.required, Validators.minLength(2)]], | |
| lastName: ['', Validators.required, Validators.minLength(2)], | |
| age: ['', [Validators.required, Validators.min(18)]] | |
| }); | |
| this.profileForm.controls['firstName'].events.subscribe((event) => { | |
| if(event instanceof StatusEvent) { | |
| console.log(event.status); | |
| } | |
| if(event instanceof PristineEvent) { | |
| console.log(event.pristine); | |
| } | |
| if(event instanceof TouchedEvent) { | |
| console.log(event.touched); | |
| } | |
| if(event instanceof ValueChangeEvent) { | |
| console.log(event.value); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment