2017-05-05 59 views
0

我想增加我的輸入框, 它的遞增和拼接,但問題是當我在一個框中輸入值相同被替換在other.I認爲它歸因於ngModel 。任何人都可以幫助我.Thnaks。如何增加輸入框的添加按鈕

<div class="form-group row text-right" *ngFor='let row of vipInput'> 
    <label class="col-sm-3 form-control-label m-t-5" for="password-h-f"></label> 
    <div class="col-sm-9 form-control-label m-t-5" for="password-h-f"> 
     <div class="row"> 
      <div class="col-sm-9" > 
       <input class="form-control" [(ngModel)]="data.vipInput" id="email" type="email"> 
      </div> 
      <div class="col-sm-3"> 
       <button class="btn btn-danger" (click)="removeVipInputs(index)">Remove</button> 
      </div> 
     </div> 
    </div> 
</div> 
    <button type="button" class="btn btn-secondary" (click)="vipInputIncrement('val')"> 
      <i class=" fa fa-plus"></i> 
    </button> 

我的TS,(我用angular2)

vipInputIncrement(val) { 
    this.vipInput.push({'val':''}); 
} 
removeVipInputs(k) { 
    this.standardInput.splice(k,1); 
} 
+0

我想你可以使用數組的$ index來區分每個元素的ng模型。例如[(ngModel)] =「data.vipInput [$ index]」 – CrazyMac

+1

您綁定到'data.vipInput',它不依賴於'row',因此每行都是一樣的。 –

+0

嗨,Gunder,如何解決這個問題。 – MMR

回答

0

你的NG-模式應該是此行裏面的東西,

<div class="form-group row text-right" *ngFor='let row of vipInput'> 
    <label class="col-sm-3 form-control-label m-t-5" for="password-h-f"></label> 
    <div class="col-sm-9 form-control-label m-t-5" for="password-h-f"> 
     <div class="row"> 
      <div class="col-sm-9" > 
       <input class="form-control" [(ngModel)]="row.val" id="email" type="email"> 
      </div> 
      <div class="col-sm-3"> 
       <button class="btn btn-danger" (click)="removeVipInputs(index)">Remove</button> 
      </div> 
     </div> 
    </div> 
</div> 

我覺得應該是row.val,但你可以決定你想要顯示的內容