3

Please have a look at the Plnkr below. If you uncomment the html tag in the app.html, all the kids will be Tim.

<!--<form #form="ngForm">-->
  <div class="row">
    <div class="col-lg-12" *ngFor="let employee of employees">
      <div class="col-lg-6">
        <label for="employee">Employee</label>
        <input type="text" class="form-control" id="employee" [(ngModel)]="employee.firstName" name="employee">
      </div>
      <div class="row">
        <div class="col-lg-12" *ngFor="let kid of employee.kids" style="border:1px solid #cecece;">

          <div class="col-lg-4">
            <label for="kid">Kid - {{kid.name}}</label>
            <input type="text" class="form-control" id="kid"  [(ngModel)]="kid.name" name="kid">
          </div>

        </div>


      </div>
    </div>
  </div>
<!--</form>-->

https://plnkr.co/edit/AyOVBXAoa8kuE1CQf6XB?p=preview

I would like to use the Angular2 Form feature so it is not an opption to skip it and replace it with some javascripts.

4

1 回答 1

4

在 for 循环中添加[ngModelOptions]="{ standalone : true }"到您的输入以使它们都是唯一的(更准确地说,它们不会被添加到同一个FormGroup实例中):

<input type="text" class="form-control" id="kid"  [(ngModel)]="kid.name" 
[ngModelOptions]="{ standalone : true }" name="kid">
于 2017-01-16T15:56:01.297 回答