PatchValue method in Angular allows updating the value of a form control field, but cannot deactivate the field. However, we can add an additional property to track if the field is disabled or not.
For example, let's say we have a FormGroup with a FormControl called "name". We can add a new boolean property called "disabled" to it like this:
this.formGroup = this.formBuilder.group({
name: [{value: '', disabled: false}]
});
Now, when we want to both assign a value and deactivate the field, we can use patchValue as follows:
this.formGroup.patchValue({
name: {value: 'John Doe', disabled: true}
});
This will assign the value of "John Doe" to the "name" control and disable it as well.
Asked: 2023-05-25 15:35:54 +0000
Seen: 1 times
Last updated: May 25 '23