I’ve a piece of code in ts file as follows
totalCrList : number = 0;
tabs: []
ngOnChanges() {
//some code
this.someFunc();
this.tabs.push({
"id": "tab3",
"title": `Associate / Linked Change Request (${this.totalCrList ? this.totalCrList : ''})`, //always displays '0'
"active": false
})
}
someFunc() {
this.someService.someObservable.subscribe(resp => {
this.totalCrList = resp.reduce((count, changeReq) => count + changeReq.values.filter( val => val.crId ).length, 0); //Here I get the value
this.cdr.detectChanges();
})
}
In UI this.totalCrList always displays 0
. The variable value is not updated, even I tried detectChanges. I’m not sure why its not updating the value
2
Answers
RxJS Use RxJS to update the view A
BehaviorSubject
allows you to define a default value (makes sense in your example, but does not necessary be the right choice all the time), also buffers the last emitted value.Your
this.tabs.push(//...
happens before the async action is complete. You could, for example, modify the code sosomeFunc
returns a stream: