带Firebase的角材料表为空

时间:2019-12-11 21:14:50

标签: angular firebase

我正在尝试在有角度的材质表中显示Firebase中的某些项目。我将商品(汽车)带到一个数组中,然后在数据源中发送该数组,但从未显示商品。这是我的代码:

ngOnInit() {
this.dbService.getCars().subscribe((cars) =>{
  this.cars.length = 0;
  cars.map((car:any)=>{
    this.cars.push(car.payload.doc.data());
    this.dataSource = new MatTableDataSource(this.cars);
  });

});
}

服务中还有getCars:

getCars(){
return this.dbFirestore.collection('cars').snapshotChanges();
}

1 个答案:

答案 0 :(得分:0)

在更新数据后尝试实施变更检测。

constructor(private changeRef : ChangeDetectorRef) { }

this.dbService.getCars().subscribe((cars) =>{
  this.cars.length = 0;
  cars.map((car:any)=>{
    this.cars.push(car.payload.doc.data());
    this.dataSource = new MatTableDataSource(this.cars);
  });

    //This line updates the UI with new changes in data
   this.changeRef.detectChanges();
});