Hi I am trying to pass a object from Component A to another sibling Component B and I am using service to do so and I am using Subject. But when calling the get method from service I am not able to get the data in Component B. I don't know what I am doing wrong. Any help would be great. My code is as below -
Hierarchy Service
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
@Injectable()
export class HierarchyService {
private dataObj = new Subject;
getDataObj = this.dataObj.asObservable();
constructor () {}
setData(setData:Object){
console.log("setData +++ ", setData);
this.dataObj.next(setData);
}
}
Component A Inside a event handler I am setting below code -
this.hierarchyService.setData(treeModel);
Component B inside ngOnInit I am trying to get the data that I set in Component A
this.hierarchyService.getDataObj.subscribe(data => console.log("data ", data));