Reputation: 565
Data is changing in service and component but it's not getting reflected on view.
Following are the solutions used which didn't solve the issue :
Used lifecycle hook ngDoCheck
Used angular zone and called method inside zone.run()
Invoked change detection manually by calling ChangeDetectorRef.detectChanges()
Added timer of 5 sec before calling the methods.
My code:
ngOnInit() {
this.noticeService.notices.subscribe(data => {
this.notices = data;
});
}
The issue is whenever I debug the code I found that data is reflected in notices variable of component but its not getting displayed on UI unless any user action like click is not performed. Your help is much appreciated.
Upvotes: 2
Views: 1606
Reputation: 565
I have solved the issue just by moving my subscription code from ngOnInit() to constructor(). I don't know why it works only in constructor and also not found any relevant information regarding it. I also found that someone has already faced the same issue.
Why observable.subscribe works only from constructor
Upvotes: 3