【问题标题】:Angular - Updating to FirebaseAngular - 更新到 Firebase
【发布时间】:2019-03-01 11:23:03
【问题描述】:

我想更新表单中的值。问题是,我不确定如何获取表的 firebase 键。我有一个按钮,单击该按钮将在表单上显示值。下一步是进行更改,然后单击更新按钮将其保存到 Firebase。

我已经这样做了,它可以工作,但这绝对不是解决方案,因为它会导致页面出现问题。

 <button *ngFor="let content of educates" id="updateBtn" class="btn btn-light" (click)="Update(content.key)" style="visibility: hidden">Update</button>

获取 Firebase 密钥的最佳方式是什么?

我的组件.ts

Update(key: string) {

    this.educateSrv.getAll();
    let _educate = new Educate();

    _educate.roles = this.Form.value.roles;
    _educate.widgets = this.Form.value.widgets;
    _educate.url = this.Form.value.url;
    _educate.title = this.Form.value.title;
    _educate.status = this.Form.value.uploadWhen;
    _educate.updatedBy = this.currentUser;
    let _date = new Date()
        .toDateString()
        .replace(/^\S+\s/, '');
    _educate.updatedAt = _date;

    this.educateSrv.Update(key, _educate);
}

我的服务.ts

Update(id: string, data:Educate) {
    return this.af.object("/educate/" + id).update(data);
}

提前致谢。

【问题讨论】:

  • 在读取数据的地方添加代码。
  • @BalajKhan 你是什么意思?我的 component.ts 中的 Update 函数能够获取在我的表单中输入的值。问题是获取所选项目的密钥
  • 当您从 firebase 读取数据时,数据中不存在密钥,您必须将密钥与数据连接起来。
  • @BalajKhan 是的。我能知道我该怎么做吗?

标签: angular firebase firebase-realtime-database


【解决方案1】:

您可以通过返回数据来获取密钥:

data: any;

readData() {

   this.db.list('listname').snapshotChanges().pipe(map(list => {
       return list.map(items => {
         return {key: items.payload.key, ...items.payload.val()}
       })
    })).subscribe(data => {
       this.data = data;
       });

}

【讨论】:

    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2017-01-20
    • 2019-10-18
    相关资源
    最近更新 更多