简体   繁体   English

如何获取在Angular2中动态创建的文本框的值

[英]How to get the values of text boxes which are dynamically created in Angular2

I have a requirement to design the multiple text boxes based on user clicks shown in below image 我需要根据下图所示的用户点击来设计多个文本框

design of my html page 我的html页面的设计

But whenever i click on + button value in text box is repeating my html is code like below 但是每当我点击文本框中的+按钮值重复我的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>

How to avoid this and how to use dynamic value for [(ngmodel)] . 如何避免这种情况以及如何为[(ngmodel)]使用动态值。

Don't use the same variable for all text boxes 不要对所有文本框使用相同的变量

[(ngModel)]="cli.cmd"

You can either use index 您可以使用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>

or store the value in the *ngFor interator variable rowplus 或将值存储在*ngFor interator变量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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM