2016-11-14 46 views
0

動態創建我要設計基於以下圖像如何獲得的,其在Angular2

design of my html page

顯示用戶點擊多個文本框的要求,但文本框中的值,每當我點擊+在文本框按鈕值重複我的HTML是像下面

<div class="form-group" style="margin-top: -10px;"> 
    <div class="col-lg-10" *ngFor="let rowplus of rowplus" style="margin-top: 12px;"> 
     <input type="text" placeholder="Command" class="form-control" [(ngModel)]="cli.cmd[rowplus[0]]" name="cmd" /> 
    </div> 
    <div class="col-lg-2" style="margin-top: 12px;"> 
     <button class="btn btn-success" (click)="addrow()"><i class="fa fa-plus"></i></button> 
     <button class="btn btn-danger" (click)="subrow()"><i class="fa fa-minus"></i></button> 
    </div> 
</div> 

如何避免這種代碼以及如何使用動態值[(ngmodel)。

+0

只是*不要*將它們全部綁定到同一個事物上?你能給出一個小小的背景嗎? – jonrsharpe

回答

0

不要使用相同的變量的所有文本框

[(ngModel)]="cli.cmd" 

您可以使用index

<div class="col-lg-10" *ngFor="let rowplus of rowplus; let idx=index" style="margin-top: 12px;"> 
    <input type="text" id="command" placeholder="Command" class="form-control" [(ngModel)]="cli[idx].cmd" name="cmd" /> 
</div> 

或值存儲在*ngFor迭代符變量rowplus

<div class="col-lg-10" *ngFor="let rowplus of rowplus" style="margin-top: 12px;"> 
    <input type="text" id="command" placeholder="Command" class="form-control" [(ngModel)]="rowplus.cmd" name="cmd" /> 
</div> 
+0

它不工作即時通訊使用索引zone.js時,即時獲取錯誤:346錯誤:未捕獲(承諾):錯誤:應用程序/ cli/cli.component.html:29:105中的錯誤引起:無法讀取屬性'cmd '的未定義(...)................ rowplus是一個數組,它只是有一些虛擬值爲每個添加按鈕點擊im推動一些虛擬值到rowplus給我新的排。 – RaviTeja

+0

然後'cli'可能不是一個數組,或者它是一個數組。請添加更多關於你想要完成的細節。 –

+0

CLI是類等級CLI的」對象{ 構造( 公共名稱:串, 公共地址:串, 公共密碼:串, 公共CMD:串[], 公共原因:串 ){} } 「在組件類中,聲明下面的cli對象」cli = new CLI('','','',[],'');「 – RaviTeja