带有复选框的Angular 2 Reactive Form

时间:2017-02-26 11:56:30

标签: javascript angular reactive-forms

最近,我一直试图弄清楚这些Reactive Forms的工作原理。基本的例子(没有嵌套等)是可以理解的,但是如果我有一个这样的表单结构,带有复选框:



X = ['1','2','3','4','5','6', 7','8','9','10']
index = 0
for i in range(9):
     X1 = X[i:(i+2)]
     X2 = X[]




如何在模板上呈现它以更改" *已检查" value(true | false)?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

  <form #theForm="ngForm" (ngSubmit)="submitForm(theForm.value)">

      <div *ngFor="let cb of data.checkboxes">
        <label>
          <input type="checkbox" [name]="cb.name" [(ngModel)]="cb.checked">{{cb.value}}
        </label>
      </div>

    <button type="submit" class="btn btn-primary">Submit</button>
  </form>

  <pre>{{data.checkboxes|json}}</pre>

见Plunkr:https://plnkr.co/edit/MpSO21fIJq1DtJoXmE3V?p=preview