I am trying to share service to a component, for this, I have created service.ts where the code looks like:
import { Subject } from 'rxjs/Subject';
export class CommonService {
CommonList = new Subject<any>();
}
Of course, this service is in provider list in app.module.ts Then, I have a component sidebar. From sidebar component i am subscribing this service:
ngOnInit() {
this.subscription = this.CommonServicePrivate.CommonList.subscribe(
(result: any) => {
console.log(result);
}
);
}
Here is the problem, If I try to add some list in this service from app.component.ts via some method:
click(){
this.CommonService.CommonList.next([{"some_key":"some_value"}]);
}
This actually works, but when i try this:
ngOnInit(){
this.CommonService.CommonList.next([{"some_key":"some_value"}]);
}
it does not work.
The point is that I want to pass this service to a component on load application.
In case of ngAfterViewInit, when i am trying to render this result to some template, i am getting error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value:
If someone has this experience could you please share to me. Thanks.