【问题标题】:load firebase data in angular 4 table在 Angular 4 表中加载 Firebase 数据
【发布时间】:2018-07-30 16:35:14
【问题描述】:

我正在为我使用 firebse 的数据库开发 angular 4 项目。我在其中使用 angular 的智能表来删除表数据的功能,它需要每条记录的 firebase 键,所以我从 firebase 获取它,但我无法在我的表中加载该数据以下是我的代码

我在服务中有如下写函数

getEData(): Observable<any[]>  
 {
    return this.af.list('/enquirydata').snapshotChanges();
 }

table页面的component.ts如下

 source: LocalDataSource = new LocalDataSource();


  items: Array<any> = [];


  constructor(private service: SmartTableService, private router: Router) {
 this.service.getEData().subscribe(k=> {  
  k.map(c=> {
  console.log(c.payload.val());

  this.items.push(c);
  })
  console.log(this.items);
  });


    this.source.load(this.items);
    console.log(this.source);

    } 

这里c.payload.val() 包含实际数据,而c 包含带有元数据的数据,例如密钥和其他数据。

现在我无法将这些数据加载到我的表中,我该怎么做?

【问题讨论】:

    标签: angular typescript firebase firebase-realtime-database angularfire2


    【解决方案1】:

    试试这个方法

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

    【讨论】:

    • 在component.ts中应该怎么称呼
    • 和你现在打电话的方式一样,时间 c.payload.val() 不起作用。
    【解决方案2】:

    Observable 和 Subscribe 异步操作。

    调用 this.source.load(this.items) 时,this.items 中可能没有任何内容。

    尝试更改为同步模式(不可观察)

    或者检测this.items.push(c)何时被调用,调用this.source.load(this.items)

    或将 this.source.load(this.items) 放在 this.items.push(c) 之后。如下图。

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

    【讨论】:

    • 你能给出解决方案吗
    • 你能帮我删除功能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 2018-04-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2014-03-24
    相关资源
    最近更新 更多