【发布时间】:2019-08-05 12:53:23
【问题描述】:
我正在使用 mat-checkbox 和 ngFor 从 Firestore 迭代一个数组
默认情况下,每个mat-checkbox 的值都被禁用,即false。我想要的是,当检查一个或多个框并更新页面时,这些框必须保留标记,即,true。有可能吗?
发生的情况是,我正在对每个 mat-checkbox 进行总和,并且我希望页面中的任何更改都不会影响该总和。
我在 Firestore 中的数组如下所示:
paquetes: {
paqueteId1: {
...
extras: [
{nombre: 'nombre1', isChecked: false, costo: 10},
{nombre: 'nombre2', isChecked: false, costo: 20},
{nombre: 'nombre3', isChecked: false, costo: 30}
]
}
...
}
component.html
<mat-checkbox *ngFor="let extra of paquete.extras" class="nmb" [(ngModel)]="extra.isChecked">
{{ extra.costo}}
</mat-checkbox>
<span>{{total}}</span>
component.ts
get total() {
return this.paquete.extras.filter(x => x.isChecked).reduce((a, b) => a + b.costo + 0, 0) * this.numeroPersonas + this.subtotal;
}
this.fs.getPaquete(this.idPaquete).subscribe( res => {
this.paquete = res.data();
}
);
【问题讨论】:
-
您可以将 [(ngModel)]="extra.isChecked" 更改为 [ngModel]="extra.isChecked" 。这不会检测到组件的变化,您的总和不会受到影响
-
没用,很抱歉。检测组件的变化。
-
你能展示一下你的 this.paquete 数组是什么样子的吗?
-
您似乎没有将任何内容写回数据库。您必须使用用户所做的更改来更新 firestore 数据库。如果您不更新数据库,您将始终在页面重新加载时从数据库中加载相同的数据。
标签: angular typescript google-cloud-firestore