Normally I always binded the data in the constructor:
export class AdminComponent {
parentKey: any;
childdatasource
constructor(
public firebase: AngularFireDatabase,
private fns: AngularFireFunctions,
private firebasedb: FirebaseService,
private toast: HotToastService
) {
firebase
.list('/main')
.valueChanges()
.subscribe((users) => {
this.storeExpandedIndexes();
this.mainGrid.dataSource = users; //intial data binding to grid
});
firebase
.list('/main')
.snapshotChanges()
.subscribe((users) => {
this.storeExpandedIndexes();
this.mainGrid.dataSource = users; // sync server data changes to grid
});
}
But when moving between routes I get empty data rows rendering in.
The way I find to solve this is to add another initial data binding in the ngoninit
ngOnInit(): void {
this.firebase
.list('/main')
.valueChanges()
.subscribe((users) => {
this.storeExpandedIndexes();
this.mainGrid.dataSource = users; //intial data binding to grid
});
}
but I can not remove it from the constructor because then it breaks on page refresh and does the same issue.
Is there a bit more of an elegant way to make sure data binds at the correct time instead of me adding .valuechanges() to both constructor and ngonint?