I have been using BehaviorSubject to share data between components.
Say, relative url /article belongs to ArticleComponent. Then comes /article/list in ArticlelistComponent which contains a table with list of article details.On click of any row, i need that row detail to be passed to all the other component and that's my agenda.
Issue i face is,ngOnInit works only on page reload. Initially,only default {} gets reflected in articlecomponent which is nothing but the default result i have set for BehaviorSubject.WHen i click on a row in table, that data doesnot get updated and subscribed automatically in ngOnInit.the old value {} is retained throughout .I ain't sure where to place it inorder to make the changes reflect immediately. Please help resolving.
My code is below:
articlelist.component.ts
getSpecificDetail(value){
this.articleService.getArticleDetail(value);
}
article.service.ts
public articledata=new BehaviorSubject<Object>({});
currentdata=this.articledata.asObservable();
getArticleDetail(data){ //data comes from articlelist.component
return this.articledata.next(data);
}
article.component.ts
ngOnInit(){
this.articleService.currentdata.subscribe(data=>{
this.data=data;
console.log(this.data);
})
}
Edit: I have extended my observation in Angular2:RxJS Subject not responding to events