为什么这种角度绑定不起作用

Why is this angular binding not working?

本文关键字:绑定 不起作用 为什么      更新时间:2023-09-26

我有以下html

<input type="checkbox" name="person" [(ngModel)]="person.selected" />

这显示在ngFor循环中。出于测试目的,我输出selected值,有些为true,有些为false,尽管如此,所有复选框都被选中。

绑定语法有什么错误吗?

问题是我在所有复选框上都使用了相同的name。使用唯一的名称实现了

我认为使用绑定没有任何问题,请确保selected是真是假。

   @Component({
     selector: 'my-app',
     template: `
     <h1 class="title">Simple component</h1>
      <div *ngFor="let person of persons" >
         {{person.name}}
         <input type="checkbox" name="person" [(ngModel)]="person.selected" />
      </div>
    `
    })
    export class AppComponent {
    persons = [
     {
      name: "first",
      selected: true
     },
     {
      name: "second",
      selected: false
     },
     {
      name: "third",
      selected: false
     },
     {
      name: "fourtch",
      selected: true
     }
    ]
  }

普朗克来了!