I have created a behaviour subject in a service class.
public personObject: BehaviorSubject = new BehaviorSubject({
Service
@Injectable()
export class DataService {
private _dataListSource: BehaviorSubject = new BehaviorSubject([]);
dataList: Observable = this._dataListSource.asObservable().distinctUntilChanged();
getDataList(): Observable {
return this.httpService.get('/data').map(res => {
this._dataListSource.next(res);
});
}
}
TS file
export class DataComponent implements OnInit {
public dataList$: Observable;
constructor(public dataService: DataService) {}
ngOnInit() {
this.dataList$ = this.dataService.dataList;
this.dataService.getDataList().subscribe();
}
}
HTML file
{{ data | json}}