1 回答
TA贡献1860条经验 获得超8个赞
因此,您似乎没有使用 Angular 强大的更改检测来捕获所有这些值更改。这就是我要做的:
在组件中(请添加您自己的类型):
firstToggle: boolean = true;
listItems = [
{
id: 'Toggle_one',
checked: true,
text: 'Toggle 1'
},
{
id: 'Toggle_two',
checked: true,
text: 'Toggle 2'
},
{
id: 'Toggle_three',
checked: true,
text: 'Toggle 3'
},
{
id: 'Toggle_four',
checked: true,
text: 'Toggle 4'
},
]
onToggleChange(id: string, value: any) {
this.listItems = this.listItems.map(item => {
if (item.id === id) {
item.checked = value.checked;
} else {
if (this.firstToggle) {
item.checked = false;
}
}
return item;
})
if (this.firstToggle) {
this.firstToggle = false;
}
this.saveInStore();
}
onAllChange(value: boolean) {
this.listItems = this.listItems.map(item => {
item.checked = value;
return item;
})
this.saveInStore();
}
saveInStore() {
this.store.dispatch(UpdateToggleFilter({ toggleFilters: this.toggleFilters }));
}
在 HTML 中:
<mat-list class="list lt-checkbox" [ngClass]="{'selected-all': allTogglesSelected()}">
<mat-list-item>
<app-icon-switch class="toggle-all" text="All Sydney" [subText]="" icon="" (checkedChange)="onAllChange($event)"
[checked]="allTogglesSelected()">
</app-icon-switch>
</mat-list-item>
<mat-list-item *ngFor="let item of listItems">
<mat-checkbox [checked]="item.checked" (change)="onToggleChange(item.id, $event)" color="primary"
class="toggle">{{item.text}}
</mat-checkbox>
</mat-list-item>
</mat-list>
希望这有帮助! :)
- 1 回答
- 0 关注
- 108 浏览
添加回答
举报