0

Ich arbeite an Angular Reactive Formen. Anfangs habe ich ein Formarray in Formgroup. danach schiebe ich formgroup in formarray, um die Formularsteuerung dynamisch hinzuzufügen. Ich bekomme ein Problem beim Binden dieses Steuerelements mit formControlName. Ich erhalte diese Fehler: keine Kontrolle mit Pfad: 'controlArray -> 0 -> value'Problem beim Registrieren von Kontorl mit Formular in reaktiven Kurvenformen

hier ist der Code meiner Komponentenklasse:

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

Zuerst erhalte ich Daten in formsDb, dann ich Ich suche nach dem letzten Standardformular.

showDefaultForm(){ 
     for(let form of this.formService.formsDb){ 
      if(form.formName==this.formService.defaultForm.formName){ 
       this.selectedForm = form; 
       this.addForm(); 
      } 
     }  
    } 


addForm(){    
     for (let control of this.selectedForm.controlsArr){        
      (<FormArray>this.reviewForm.get('controlArray')).push(this.fb.group([{ 
       controlType: control.controlType, 
       label: control.label,    
       value: '' 
      }])); 
     }   
    } 

Hier mein Template-Code ist:

<form [formGroup]="reviewForm" (ngSubmit)="onSubmit()"> 
    <div class="example-label"> 
    <span class='block'>    
    <div formArrayName="controlArray"> 
     <div *ngFor="let control of reviewForm.get('controlArray').controls; let i = index"> 
     <div [formGroupName]="i"> 
      <table> 
      <tr> 
       <span *ngIf="control.value"> 
        <td> 
        <label>{{control.value.label}}</label> 

       </td> 
       <td><span *ngIf="control.value.controlType == 'select'">     
        <md-select placeholder="{{control.value.label}}" formControlName="value"> 
         <md-option *ngFor="let option of control.value.options; let j=index" 
         [value]="option">{{control.value.options[j]}}</md-option>     
        </md-select> 
        </span></td> 
       <td> <span *ngIf="control.value.controlType == 'text'"> 
      <md-form-field class="example-full-width"> 
       <input mdInput type="text" placeholder="{{control.value.placeholder}}" formControlName="value" > 
      </md-form-field> 
     </span></td>       
       </span> 
      </tr> 
      </table> 
     </div> 
     </div>  
    </div> 
    </span> 
    </div> 
</form> 

Gibt es eine Frage in meinem Template-Code, Bitte helfen. Danke

+0

Welche "this.selectedForm" Variable hat? –

+0

es hat Formulardaten, d. H. Formularname und Formularsteuerelemente. Seine Klasse ist: Exportklasse FormData { Konstruktor (public formName: String, öffentliche SteuerelementeArr: Control []) {} } –

+0

Was ist 'this.showDefaultForm()'? – Alex

Antwort

0

Ändern Sie diese untere Bedingung mit dynamischem Wert. Da die von Ihnen angegebenen Informationen minimal sind. Lassen Sie uns das unten versuchen und prüfen, ob das Problem gelöst wird oder nicht.

{{control.value[i].controlType}} 
+0

Bitte sagen Sie, wo diese Zeile? –

+0

Check HTML-Seite. Sie haben dies definiert –

+0

Ich habe soeben die Zeile {{control.value [0] .controlType}} um zu überprüfen, ob Daten in die Vorlage kommen, ist es keine Bedingung –

0

denke ich, das Problem in dieser Linie ist

<div *ngFor="let control of reviewForm.get('controlArray').controls; let i = index"> 

Bitte, es

<div *ngFor="let control of reviewForm.controls.controlArray.controls; let i = index"> 

auf

Und ändern, als Sie mit Werten arbeiten können: reviewForm.controls.controlArray.controls[i].controlType oder formControlName verwenden.

Entschuldigung Wenn ich dein Problem falsch verstanden habe. Ich nehme an, dass dieser Artikel hilfreich für Sie sein wird.

+0

Beide Ansätze sind in Ordnung, trotzdem vielen Dank für dein Feedback. –

Verwandte Themen