Angular 我的表格don';t始终选中“查找值”和“滑动切换”
我的表格有问题。在web中,所有幻灯片切换都像签入一样签入 我想问题出在Angular 我的表格don';t始终选中“查找值”和“滑动切换”,angular,typescript,angular-material,Angular,Typescript,Angular Material,我的表格有问题。在web中,所有幻灯片切换都像签入一样签入 我想问题出在patchFor(){}你能看看我的代码吗? 我尝试以下代码: ts: 显示我的价值 和html: <form [formGroup]="activeHomeboxPForm" class="col s12" materialize> <section class="example-section"> <mat-
patchFor(){}
你能看看我的代码吗?
我尝试以下代码:
ts:
显示我的价值
和html:
<form [formGroup]="activeHomeboxPForm" class="col s12" materialize>
<section class="example-section">
<mat-slide-toggle formControlName="active" value="active"
class="example-margin"
[color]="color" [checked]="checked"
(click)="onActiveHomeboxP(item.homeboxpackage_id, item.active)">
{{item.active}}
</mat-slide-toggle>
</section>
</form>
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>
{{item.active}
图片:
在控制台中看起来不错,但在html中不显示,active=1已选中,active=0未选中。请了解如何实现?是否选中幻灯片是使用
[checked]
属性定义的,因此如果您希望幻灯片反映元素的活动属性,您需要
<form [formGroup]="activeHomeboxPForm" class="col s12" materialize>
<section class="example-section">
<mat-slide-toggle formControlName="active" value="active"
class="example-margin"
[color]="color" [checked]="item.active"
(click)="onActiveHomeboxP(item.homeboxpackage_id, item.active)">
{{item.active}}
</mat-slide-toggle>
</section>
</form>
{{item.active}
编辑
这里已经完成了工作
实际操作:我打开了myform
以包含所有三个幻灯片切换,并更改了patchForm
功能:
注意模板[formGroup]=“myform[i]”中的更改
并且至少在补丁格式中有更改
:
patchForm() {
this.homeboxsp.forEach((val,i) => {
this.myform[i].patchValue(
{active: val.active == "1", homeboxpackage_id: val.homeboxpackage_id}
)
});
您的所有滑块都具有相同的formControlName
请尝试设置唯一的控件名
<div *ngFor="let item of homeboxsp;let index=i">
<form [formGroup]="myform" class="col s12">
<section class="example-section">
<mat-slide-toggle formControlName="active-{{i}}"
到底是什么问题?预期的行为是什么?似乎您使用的pathValue不正确,它的参数应该是form元素的值,但您传递了三个值的数组[1,0,0]@DicBrus请编写代码好吗?Thnx@bugs如何在html幻灯片中显示acitve=1时选中的切换和激活时未选中的切换=0@DicBrus请问,你能写一些代码,如何解决这个问题?ThnxI尝试此操作,但没有任何更改。请查看我的代码。代码看起来很混乱,但我已修改了您的代码片段,使其符合要求,请查看是否有帮助。如果我尝试此代码,但对我来说不起作用。当我删除formControlName=“active”时,我的所有值都未选中。我不明白出了什么问题(这个例子对我不适用。我尝试了其他的例子,比如在post中,你能看一下并提出一些建议吗?
@Component({
selector: 'my-app',
template: `
<div *ngFor="let item of homeboxsp; let i = index">
<form [formGroup]="myform[i]" class="col s12">
<section class="example-section">
<mat-slide-toggle formControlName="active" value="active" class="example-margin" [color]="color" [checked]="item.active" (click)="onActiveHomeboxP()"> {{item.active}}
</mat-slide-toggle>
</section>
</form>
</div>
`
})
myform: FormGroup[];
constructor(public service: Service) {
...
this.myform = [
new FormGroup(
{
'active': new FormControl('', Validators.required),
'homeboxpackage_id': new FormControl('', Validators.required)
}),
new FormGroup(
{
'active': new FormControl('', Validators.required),
'homeboxpackage_id': new FormControl('', Validators.required)
}),
new FormGroup(
{
'active': new FormControl('', Validators.required),
'homeboxpackage_id': new FormControl('', Validators.required)
})
];
}
patchForm() {
this.homeboxsp.forEach((val,i) => {
this.myform[i].patchValue(
{active: val.active == "1", homeboxpackage_id: val.homeboxpackage_id}
)
});
<div *ngFor="let item of homeboxsp;let index=i">
<form [formGroup]="myform" class="col s12">
<section class="example-section">
<mat-slide-toggle formControlName="active-{{i}}"