2016-09-08 39 views
0

我有一種窗體,點擊某個按鈕時會添加輸入。在Angular2 FormGroup中進行動態添加輸入的驗證

addInput() { 
    let input: {} = { 
     "text": null 
    }; 
    this.arr.push(domain); 
     let key = 'textC_' + (this.arr.length - 1); 
     this.form.controls[key] = new FormControl("", Validators.required) 
    } 

我使用下面的代碼來呈現的HTML:

<div class="form-group row"> 
    <label>Input</label> 
    <div class="col-sm-9"> 
     <ANY *ngFor="let item of arr; let i = index"> 
     <div class="row"> 
      <div class="col-sm-6 top5"> 
       <input type="text" formControlName="textC_{{i}}" id="name" [(ngModel)]="arr[i].text" > 
       <div class="help-block" *ngIf="!form.controls.textC_{{i}}.valid > 
       Value is required 
      </div> 
     </div> 
    </div> 
    </ANY> 
</div> 
</div> 

如何呈現使用這種方法的驗證消息。

form.controls.textC _ {{I}}。有效不工作

任何備用?

使用角RC5

回答

0

不使用表達式語法{{}}。 ngIf指令已將該分配視爲表達式。嘗試:

<div class="help-block" *ngIf="!form.controls['textC_' + i].valid"> 
+0

這個答案出現在低質量的審查隊列中,大概是因爲你沒有提供任何代碼的解釋。如果此代碼回答此問題,請考慮添加一些文字,說明答案中的代碼。這樣,你就更有可能獲得更多讚揚 - 並幫助提問者學習新東西。 – lmo