【问题标题】:after deleting data from firebase it is still showing in table in angula 4从firebase中删除数据后,它仍然以角度4显示在表格中
【发布时间】:2018-02-21 15:30:56
【问题描述】:

我正在处理 angular 4 项目,我在其中使用智能表。对于数据库,我使用的是 firebase。 我写了一个函数来删除它从火力库中删除的行数据,但其他数据显示两次(重复)。 以下是代码

我在服务中有写函数来获取如下数据

getEData() 
 {

    return this.af.list('/enquirydata').snapshotChanges().map(changes => {
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
    });
 }

也为了删除我在服务中写了以下函数

deleteEnquiry(data){
    this.af.list(`/enquirydata`).remove(data);
    console.log("item deleted");

}

现在在component.ts中我写了如下

source: LocalDataSource = new LocalDataSource();
  items: Array<any> = [];


  constructor(private service: SmartTableService, private router: Router) {
 this.service.getEData().subscribe(k=> {
  k.map(c=> {
  this.items.push(c);
      this.source.load(this.items);
  })
  });
    } 

onDelete(event) {
console.log(event);
    if (window.confirm('Are you sure you want to delete?')) {
      this.service.deleteEnquiry(event.data.key);
    } else {
      event.confirm.reject();
    }
  }

有什么帮助吗?

【问题讨论】:

  • 您正在将值推送到数组中。 this.items.push(c);。首先通过'this.array = []'清除数组
  • 它不工作,请您提供解决方案

标签: angular typescript firebase firebase-realtime-database ng2-smart-table


【解决方案1】:
 this.service.getEData().subscribe(k=> {
   this.items = [...k]; // use can use array destructuring
   this.source.load(this.items);
 });
} 

【讨论】:

    猜你喜欢
    • 2018-07-26
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2021-06-09
    相关资源
    最近更新 更多