2017-12-12 4 views
1

Ich arbeite an Angular Reactive Form. Dies ist meine Komponentenklasse Code:Ob ein FormControl-Wert ein Array sein kann?

ngOnInit(){ 
     this.reviewForm = this.fb.group({    
      'controlArray': new FormArray([]) 
     });   
     this.showDefaultForm();      
    } 

addForm() { 
    (<FormArray>this.reviewForm.get('controlArray')).push(this.fb.group({ 
     controlType: control.controlType, 
     options: control.options, 

    })); 
} 

I Typeerror bin immer: this.validator keine Funktion mit diesem ist. Ich denke, das liegt an der Zuweisung eines String-Arrays (d. H. Control.options) als Wert an FormControl. Wenn ich es zu einem FormArray mache, dann behebt dieser Fehler, aber ich habe ein Problem, um es als FormArray in Vorlage zu behandeln. Bitte sagen Sie mir, wenn ich FormArray nicht verwende, kann ich FormControl ein String-Array als Wert zuweisen? Wenn nein, dann wie man es als FormArray in der Vorlage behandelt. Danke

Antwort

1

Schließlich habe ich es gelöst, und die Antwort ist ja. Der Wert eines FormControl kann ein Array sein. Dafür müssen Sie den Wert in eckigen Klammern [] einschließen. Darüber hinaus sind diese eckigen Klammern für einfache Werte (Nicht-Arrays) optional, dh Sie können den Wert innerhalb oder ohne eckige Klammern einschließen.

-Code Erklärung:

(<FormArray>this.reviewForm.get('controlArray')).push(this.fb.group({ 
     controlType: control.controlType, 
     options: [control.options], //previously I was not using square brackets with options value i.e. options: control.options which was wrong. 

    })); 
Verwandte Themen