【问题标题】:how to show in html slide-toggle checked when acitve = 1 and not checked when active=0?如何在 html 幻灯片切换中显示当 acitve = 1 时检查而在 active = 0 时不检查?
【发布时间】:2018-09-15 01:34:48
【问题描述】:

我的 mat-slide-toggle 有问题。在网络中,我所有的滑动切换都像在 .image 中一样检查

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

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 中不显示,活动 = 1 已选中,活动 = 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>

显示这个:

【问题讨论】:

  • 你到底想要什么?显示滑块处于活动状态 = 1,否则将其隐藏?
  • [checked]="item.active === '1'" ---你确定active后面的值是一个字符串吗?如果你这样比较会发生什么? [选中]="item.active == '1'"
  • @DiabolicWords 我的活动是数字。我尝试 [checked]="item.active ==1" 但没有任何改变
  • @David 我想在 html 幻灯片中显示选中的活动为 1,没有选中的活动 = 0

标签: angular typescript angular-material slidetoggle


【解决方案1】:

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

【讨论】:

  • 大卫,在页面显示良好,我的产品活动被选中,产品活动0没有被选中。但在控制台显示错误,如更新帖子中。谢谢
  • 大卫你好吗?是的,我知道这是不同的问题,但请,我不能发表新帖子。在这篇文章中,我只需要在按 OK 时更改开关。 stackblitz.com/edit/angular-lmznlu?file=app/app.component.ts谢谢
  • 你好吗?我在分页中导航时遇到此代码问题。当我在第 2 页或第 3 页中导航时,显示所有活动或不活动的幻灯片切换,如第 1 页 :( 请问,有什么问题?我需要你的帮助
  • 再开个问题我看看
  • stackoverflow.com/questions/50398726/… 我的新任务。我创建了新帐户,因为我已达到我的问题限制。非常感谢
【解决方案2】:

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

打字稿:

    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

【讨论】:

  • 我试过你的代码,效果很好,但是当我在分页中导航时出现问题。因此,如果我在分页 2 中,当我在此分页中激活产品时,我的所有产品都被激活(仅选中),当我停用产品时,所有产品都被停用。拜托我需要你的帮忙。谢谢
  • 您能否更新上述演示以演示发生了什么?有点不清楚发生了什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 2022-12-17
  • 2023-03-22
  • 1970-01-01
  • 2017-01-01
相关资源
最近更新 更多