In the other examples at StackOverflow there are many questions about using FormGroups in FormArrays. But my question is the opposite.
FormArrays have a push method, that makes many things possible. FormGroups have indeed an addControl method for adding simple FormControls. AFAIK FormGroups do not have addFormArray
or addFormGroup
method. Therefore I need your help.
Following situation:
this.myForm = this.fb.group({
id: this.fb.control([this.book.id]),
// or short form `id: [this.book.id],`
});
Adding a simple control at a later point in time is easy:
this.myForm.addControl('isbn', this.fb.control(this.book.isbn));
But what about adding FormArrays and FormGroups into an existing FormGroup? For instance, I would like to use the following array and object for this purpose:
const authors = ['George Michael', 'Aretha Franklin'];
const metaData = { description : 'Great Book', publication: 2019}
I would like to add authorsArray
or metaData
only then if they are existing. That's the reason, why I want to add them at a later time.
p.s. Please ignore the validation rules.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…