【发布时间】: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