Reputation: 6262
Using angular, I have a grid where user selects the data by clicking on the checkbox. I store the selection in an array.
Below is the code of selecting checbox
getCheckedItemList() {
let selectedData: any[];
//loop through array of data, find selected and populate selectedData array.
//Below line sets selected data to the service
this.myService.setSelectedData(this.selectedData);
}
And below is the service as:
export class MyService {
public selectedData = new Subject<any[]>();
public selectedDataObservable = this.selectedData.asObservable();
setSelectedData(data: any[]) {
this.selectedData.next(data);
}
}
The above code all works fine.
The problem I am having is how to get this saved data from my service.
I tried to do something like:
this.myService.selectedData
But this does not work.
Could anyone point how can I get the data?
Thanks
Upvotes: -1
Views: 62
Reputation: 6262
You can change your service to use BehaviorSubject as
export class MyService {
public selectedData = new BehaviorSubject<any>();
public selectedDataObservable = this.selectedData.asObservable();
setSelectedData(data: any[]) {
this.selectedData .next(data);
}
}
And you can get this value as:
this.myService.selectedData.value
Upvotes: 0