【问题标题】:Checkbox field immediately gets unchecked after checking in Angular/Ionic app签入 Angular/Ionic 应用程序后,复选框字段立即取消选中
【发布时间】:2022-01-11 20:54:12
【问题描述】:

我有一系列数据在我的应用程序中循环。每个项目都包含字段checked,表示是否应选中该项目的复选框。默认情况下,在我的list.html 文件中设置为checked: false

<ion-item lines="full" *ngFor="let item of items">
    <ion-label [class.strike]="item.checked" class="ion-padding ion-text-wrap">
        {{ item.title }}
    </ion-label>
    <ion-checkbox slot="start" [(ngModel)]="item.checked" mode="ios" (ionChange)="itemChecked(item)"></ion-checkbox>
</ion-item>

在更改使用ionChange 选中的复选框时,我调用我的itemChecked 方法将我的item 传递给它。

async itemChecked(item: Item) {
    console.log(item); //I can see checked has been updated.
    await this.firestore.updateItemKey(item, 'checked');
}

然后,在我的firestore 服务文件中,我像这样更新文档:

updateItemKey(item: Item, key: string) {
    const itemDocRef = doc(this.firestore, `items/${item.id}`);
    return updateDoc(itemDocRef, { key: item[key] });
}

但是,回到我的 list.html 文件中,该项目没有被检查,并且我的 firebase 数据库中没有任何内容被正确更新?该项目很快显示为已选中,然后立即恢复为未选中。

对于我可能忽略或错误操作导致此问题的任何想法?

【问题讨论】:

    标签: javascript angular firebase ionic-framework google-cloud-firestore


    【解决方案1】:

    我认为您应该使用[checked]="item.checked" 而不是[(ngModel)]="item.checked",而在itemChecked() 中使用item.checked = !item.checked,因为您想用新值更新[checked] 绑定。当然,你也应该处理 ionChange 的 checked==false 结果。

    【讨论】:

    • 谢谢你,这很有帮助,让我朝着更好的方向前进。您能否详细说明在ionChange 中处理checked==false 的含义?
    • 好吧,您正在使用“已选中”更新 firebase,但也可以通过取消选中该复选框来触发 ionChange,因此您将再次使用“已选中”进行更新。
    • 我想我正在关注。我基本上想捕获该项目的checked 的当前值并将其保存在firebase 中。
    • 是的,对不起,你可能是对的,我不太喜欢 firebase。
    • 不用担心。这是我第一次使用它。我很欣赏洞察力和指示。标记为已接受的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2018-09-12
    • 2018-09-14
    相关资源
    最近更新 更多