Issue
I am receiving data in constructor using where
condition from Firestore.
When I console.log
I see my array but it does not reflect on viewport home.html till I click anywhere on page or change browser window [ like open some other window and come back to this window].
My friend guided me to use ChangeDetectorRef
and I used it as guided by her. I have called this.cd.detectChanges();
when all of the data is fetched any my array is ready, but it did not work.
Below is my code::
home.ts
import { Component, OnInit } from "@angular/core";
import * as firebase from "firebase";
import { firestore } from "firebase/app";
import {
AngularFirestoreCollection,
AngularFirestore,
} from "@angular/fire/firestore";
import { ChangeDetectorRef } from "@angular/core";
@Component({
selector: "app-test1",
templateUrl: "./test1.page.html",
styleUrls: ["./test1.page.scss"],
})
export class Test1Page implements OnInit {
dailyreports: any = [];
constructor(
private firestore: AngularFirestore,
private cd: ChangeDetectorRef
) {
let start = new Date("2020-03-29");
firebase
.firestore()
.collection("dailyreport")
.where("timestamp", ">=", start)
.onSnapshot((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.id, " ===> ", doc.data());
this.dailyreports.push(doc.data());
});
this.cd.detectChanges();
console.log(this.dailyreports);
});
}
ngOnInit() {}
}
home.html
<ion-content>
<div
class="info col-11"
*ngFor="let list of this.dailyreports; let i = index;"
(click)="openModaWithData(i)"
>
<div id="chtv">
Click here to view what {{list.name}} did.
</div>
<div id="rcorners">
{{list.name}}
</div>
<div id="rcorners2">
{{list.timestamp}}
</div>
<br />
</div>
</ion-content>
Solution
You can force angular to refresh (change-detection) based on variable re-assignment.
firebase
.firestore()
.collection("dailyreport")
.where("timestamp", ">=", start)
.onSnapshot((querySnapshot) => {
// Get new items
const newItems = [];
querySnapshot.forEach((doc) => {
console.log(doc.id, " ===> ", doc.data());
newItems.push(doc.data());
});
console.log({ newItems });
// Trigger change detection
this.dailyreports = this.dailyreports.concat(newItems)
});
Answered By - Ben Winding
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.