2016-11-14 43 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