如何在 html 中显示滑动切换在 active = 1 时选中,而在 active=0 时不选中?

how to show in html slide-toggle checked when acitve = 1 and not checked when active=0?

我的 mat-slide-toggle 有问题。在 web 中,我所有的滑动切换都像在 .image

中一样被检查

我认为所有的东西都很好,但是在html显示中全部勾选了。拜托,你能建议我任何解决方案吗?我尝试使用,就像在这个 中一样,但对我没有任何作用。 我的代码:

ts代码:

this.activeHomeboxPForm = new FormGroup({
  'active': new FormControl(true, Validators.required),
  'homeboxpackage_id': new FormControl('', Validators.required)

});
  populateFormHomeboxP() {
    this.ws.homeboxPGetAll().subscribe(
      homeboxsp => {
        this.homeboxsp = homeboxsp.map((homeboxspp) => {
        this.activeHomeboxPForm.controls['active'].setValue(homeboxspp.active),
        this.activeHomeboxPForm.controls['homeboxpackage_id'].setValue(homeboxspp.homeboxpackage_id)
          console.log(homeboxspp)
          console.log(homeboxspp.active)
          console.log(homeboxspp.homeboxpackage_id)
          return new HomeboxP(homeboxspp);

        });
      }
    )
  }

html代码:

  <tbody>
      <tr *ngFor="let item of homeboxsp; let i=index">
       <td> 
        <form [formGroup]="activeHomeboxPForm" class="col s12"> 
          <section class="example-section">
            <mat-slide-toggle  formControlName="active" class="example-margin" [checked]="item.active === '1'"> {{item.active}}
            </mat-slide-toggle>
          </section>
          </form>
      </td>
     </tr>
    </tbody>

在控制台中看起来不错,但在 html 中不显示,active = 1 已选中,active = 0 未选中。请问如何实施?

更新码:

<tr *ngFor="let item of homeboxsp; let i=index">
   <td> 
    <form [formGroup]="activeHomeboxPForm" class="col s12"> 
      <section class="example-section">
        <mat-slide-toggle  formControlName="active-{{i}}" class="example-margin" [checked]="item.active === '1'"> {{item.active}}
        </mat-slide-toggle>
      </section>
      </form>
  </td>

显示这个:

您的 html 显示所有滑块都已选中,因为您为它们提供的都是相同的 formControlName

改为

    <form [formGroup]="activeHomeboxPForm" class="col s12"> 
<tr *ngFor="let item of homeboxsp; let i=index">
   <td> 

      <section class="example-section">
        <mat-slide-toggle  formControlName="active-{{i}}" class="example-margin" [checked]="item.active === '1'"> {{item.active}}
        </mat-slide-toggle>
      </section>
      </form>
  </td>

编辑

您还可以为所有滑块使用一个唯一的表单组

let controls = {
  'homeboxpackage_id': new FormControl('', Validators.required)
};

for(let i = 0;i <  this.homeboxsp.length;i++)
{
  controls['active-'+i] = new FormControl(this.homeboxsp[i].active =='1', Validators.required)
}
    this.myform = new FormGroup(controls);

https://stackblitz.com/edit/angular-afrebm?file=app%2Fapp.component.ts

我不确定它是否有意但 Angular Material 滑动切换不能很好地与表单控件一起使用。这是一个解决方法:

打字稿:

    this.myform = new FormGroup({
      active: new FormControl(true, Validators.required),
      homeboxpackage_id: new FormControl('', Validators.required),
      inactive: new FormControl(false, Validators.required),
    });
  }

HTML

...
   formControlName="{{item.active ? 'active' : 'inactive'}}" class="example-margin"> {{item.active}}
...

DEMO