Watching state changes and being reactive
We have already learned to create forms programmatically and specify all our fields and their validations in the code. A reactive form can listen to changes in the form controls when they happen and react accordingly. A suitable reaction could be to disable/enable a control, provide a visual hint, or do something else according to your needs.
How can we make this happen? A FormControl
instance contains two observable properties: statusChanges
and valueChanges
. The first one notifies us when the status of the control changes, such as going from invalid to valid. On the other hand, the second one notifies us when the value of the control changes. Let's explore this one in more detail, using an example.
The price field in the form of the product create component contains a custom validator to check if the price is within a valid range. From an end-user point of view, it would be...