my app structure is as below, my question is how to update children components view on receiving initial or future data, imagine I have only one service which has an event OnDataUpdate, all the child components are receiving the same instance of the service since it has declared in App module providers section, on the other hand, I have tried all these ways & did not work:
- ApplicationRef.tick()
- ChangeDetectionRef.markForCheck()
- ChangeDetectionStrategy
- Shared service among components with the OnDataRecieved event which is like this
@Injectable()
export class ApiService {
public OnDataRecieved: EventEmitter<Model> = new EventEmitter<Model>();
constructor(private http: HttpClient, private ngZone: NgZone) {
}
public getDataAsync(): Observable<Model> {
return this.http
.get<Model>('url')
.pipe(catchError(er => throwError(er)));
}
}
and in App root component this is like below code
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
changeDetection: ChangeDetectionStrategy.Default
})
export class AppComponent implements DoCheck {
model: BehaviorSubject<Model> = new BehaviorSubject<Model>(new Model()); //with default values
subModel: BehaviorSubject<SubModel>;
constructor(private apiService: ApiService,
private zone: NgZone) {
this.apiService.getDashboard().subscribe((data) => {
this.zone.run(() => {
this.apiService.OnDataReceived.emit(data);
this.model = new BehaviorSubject<Model>(data);
});
});
this.model.subscribe((mdl) => {
this.subModel = new BehaviorSubject<SubModel>(mdl.subModel));
});
}
ngDoCheck() {
}
}
imagine the model is nested and propagated through the child components as data is loaded or changed, the structure can be like this
__ AppRootComponent
|_____ Component1
|_________SubCompoent1-1
|_________SubCompoent1-2
|_____ Component2
|_________SubCompoent2-1
|____________SubCompoent2-1-1
I receive the data changes in ngDoCheck, no need to trigger the detect changes, but the UI and child components does not get updated!