代码之家  ›  专栏  ›  技术社区  ›  0mpurdy

是否可以以模板驱动的形式使用布尔值数组绑定到多个复选框?

  •  0
  • 0mpurdy  · 技术社区  · 9 年前

    FormArray from reactive forms 以及如中所述的阵列或地图中的对象方法 this question .通常我会使用前者,如果绝对必要,则使用后者-但我想知道是否可以使用 只是 boolean 价值观

    样板

    <div>
      <h2>Hello {{name}}</h2>
    </div>
    <input type="checkbox" *ngFor="let item of items; let i = index;" [(ngModel)]="items[i]"/>
    

    组成部分

    @Component({
      selector: 'my-app',
      template: `...`,
    })
    export class App {
      name:string;
      items = [false, false, true];
      constructor() {
        this.name = `Angular! v${VERSION.full}`
      }
    }
    

    Gif showing the incorrect behaviour

    Live example on Plunker

    1 回复  |  直到 9 年前
        1
  •  2
  •   Aravind    9 年前

    为了避免参考问题,ngFor必须升级。

    <div *ngFor="let item of items; let i = index;" >
         <input type="checkbox" [(ngModel)]="items[i]"/>
    </div>
    

    Updated Plunker

    推荐文章