Skip to content

Instantly share code, notes, and snippets.

@rakia
Created May 22, 2024 20:25
Show Gist options
  • Select an option

  • Save rakia/d39e5c07d011235e691246a53e01601c to your computer and use it in GitHub Desktop.

Select an option

Save rakia/d39e5c07d011235e691246a53e01601c to your computer and use it in GitHub Desktop.
Angular: Unified Control State Change Events
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