StatusChanges in Angular Forms

The StatusChanges is an event raised by the Angular forms whenever the Angular calculates the validation status of the FormControl, FormGroup or FormArray. It returns an observable so that you can subscribe to it. The observable gets the latest status of the control. The Angular runs the validation check on every change made to the control. It also generates a list of validation errors in which case the status becomes INVALID. If there are no errors, then the status becomes VALID

How to use StatusChanges

The Angular Forms has three building blocks. FormControl, FormGroup & FormArray. All of these controls extend the AbstractControl base class. The AbstractControl base class implements StatusChanges event

We can subscribe to StatusChanges by getting the reference of the control and subscribing it as shown below

You can also subscribe to the top-level form as shown below.

StatusChanges Example

Create a reactive form as shown below

StatusChanges of FormControl

You can subscribe to StatusChanges of a single FormControl as shown below. Here in the newStatus variable, we will get the latest status of the firstname. You can also retreive the latest status of the firstname using this.reactiveForm.get("firstname").status

But, the top-level form is not yet updated at this point, hence this.reactiveForm.status still shows the old status of the firstname and also the form.

The statusChanges event for the firstname fires immediately after the new status is updated but before the change is bubbled up to its parent. Hence the this.reactiveForm.status still shows the old status.

You can work around this by waiting for the next tick using setTimeout as shown below.

StatusChanges of FormGroup

The StatusChanges event of FormGroup or FormArray is fired, whenever the status of any of its child controls are calculated. For Example, the following StatusChanges will fire even whenever the status of the city, state & pincode are calculated.

StatusChanges of Form

The following example show we can subscribe to the changes made to the entire form.

emitEvent & StatusChanges

The statusChanges event is fired even when the angular calculates the status of the control either via UI or programmatically. In some circumstances, you might not want to raise the statusChanges event. To do that we can use the emitEvent: false

In the following example, the statusChanges event is not fired at all, even though the value of the firstname is changed making it and the form INVALID.

You can use emitEvent: false with the setValue, patchValue, markAsPending, disable, enable, updateValueAndValidity & setErrors methods.

onlySelf & StatusChanges

When onlySelf: true the changes will only affect only this FormControl and change is not bubbled up to its parent. Hence the StatusChanges event of the parent FormGroup does not fire.

For Example, the following code will result in the StatusChanges of the firstname. but not of its parent (i.e. top-level form)

You can use the onlySelf: true with the setValue, patchValue, markAsUntouched, markAsDirty, markAsPristine, markAsPending, disable, enable, and updateValueAndValidity methods

Complete Source Code

[tabby title=”reactive.component.ts”]

[tabby title=”reactive.component.html”]

[tabbyending]

[tabby title=”app.component.html”]

[tabby title=”app.component.ts”]

[tabby title=”app.module.ts”]

[tabbyending]

StatusChanges in Template Driven Forms

StatusChanges event can also be used in the template-driven forms. All you need to do is to get the reference to the Form Model in the component as shown below

You can refer to the example code below

[tabby title=”template-component.ts”]

[tabby title=”template-component.html”]

[tabbyending]

Summary

In this tutorial, we learned how to make use of StatusChanges in Angular Forms. The StatusChanges event is fired whenever the angular calculates the validity status of the FormControl, FormGroup or FormArray. It is an observable and we can subscribe to it. The StatusChanges event does not fire depending on how we set emitEvent or onlySelf, when updating the value and validity of the form controls.

  1. Angular Forms Tutorial: Fundamental & Concepts
  2. Template Driven Forms in Angular
  3. Set Value in Template Driven forms in Angular
  4. Reactive Forms in Angular
  5. FormBuilder in Reactive Forms
  6. SetValue & PatchValue in Angular
  7. StatusChanges in Angular Forms
  8. ValueChanges in Angular Forms
  9. FormControl
  10. FormGroup
  11. FormArray Example
  12. Build Dynamic or Nested Forms using FormArray
  13. Validations in Reactive Forms in Angular
  14. Custom Validator in Reactive Forms
  15. Passing Parameter to Custom Validator in Reactive Forms
  16. Inject Service into Custom Validator
  17. Validation in Template Driven Forms
  18. Custom Validator in Template Driven Forms

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top