For the life of me, it seems like I should be able to create a disabled FormArray. I'm trying to build a view-only parent form (FormArray) with 1-n subarrays (also FormArray). I pass the subarrays down to the child components to initialize their controls (FormControl).
I've tried calling disable()
on the parent form with different options and at different hooks of the life cycle, it throws this exception:
Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.
Here's are a couple examples of some of what I've tried:
- in
ngOnInit
, both create and disable form -> does not disable, throws exception - in
ngOnInit
create form, inngAfterViewInit
disable form -> DOES disable but throws exception
An older version of forms accepted an options object, and (sadly) this no longer works but would have looked like: new FormArray({ value: myControlsArray, disabled: true })
.
I created a plnkr to model the component hierachy and form.
Does anyone know how to initialize a disabled form?
Edit (answer): I overlooked an important piece of information and clue to the solution. In my use case, I take a saved form value and initialize a new read-only form. Instead of passing the saved form value to the child component, I instead should have created the parent form and its sub-forms and controls.