I have an angular 2 model driven form that has a property of type FormArray
. When I call the myForm.reset(this._originalValue)
method, the number of items in the FormArray
is not reset to the original value.
Here is a plunker showing the problem: https://plnkr.co/edit/gRHD5Ikbm1aGzFQUF2nn?p=preview
Is there a way to force the myForm.reset()
method to reset the count of items in properties of type FormArray
? Or do I have to manually reset it?
The proper way of resetting a form is by calling the reset() method on your templateForm property. Resets the FormGroup, marks all descendants are marked pristine and untouched, and the value of all descendants to null.
In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.
import { FormsModule } from '@angular/forms'; In Reactive forms, we need to import FormGroup from '@angular/forms' . After importing the above-mentioned modules in the respective approach, angular forms module provides an inbuilt method called reset(). We can use the method and we can reset the form.
reset()linkResets the form control, marking it pristine and untouched , and resetting the value. The new value will be the provided value (if passed), null , or the initial value if nonNullable was set in the constructor via FormControlOptions .
Well, it appears this is a known issue and the Angular team has decided that folks will have to manually reset the FormArray size.
https://github.com/angular/angular/issues/10960
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With