how to show in html slide-toggle checked when acit

2019-06-14 15:50发布

I have a problem with my mat-slide-toggle. In web all my slide-toggle are checked like in .imageenter image description here

I think that all of things are good, but in html display all checked. Please, can you suggest me any solution? I try to use, like in this post but nothing work for me. My code:

ts code:

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 code:

  <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>

imageconsole

in console looks good, but in html doesn't display, active = 1 checked and active = 0 no checked. Please any idea how to implement?

Update code:

<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>

show this: error

2条回答
对你真心纯属浪费
2楼-- · 2019-06-14 16:17

I am not sure if it was intended or not but Angular Material Slide toggle dosn't work well with formcontrols. Here is a workaround:

Typescript:

    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

查看更多
Anthone
3楼-- · 2019-06-14 16:24

Your html displays all sliders as checked because you gave them all the same formControlName

Change it to

    <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>

Edit

You can also use one unique form group for all your sliders

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

查看更多
登录 后发表回答