I realise there are several questions on this on StackOverflow and I'm still working my way through them.
I am trying to access the values of a behavior subject but obviously don't understand enough to get there. I've read through the rxjs docs and various other sources. I have tried creating a new BehaviourSubject, using next, subscribing to it, etc. Also tried using pipe & map to map it out.
this.theService.items$
is an Observable - Behavior Subject, which has a private field _values, which I am trying to access but can't. And then project that to a variable so I can access the field values and use in my component.
items$ is a getter to retrieve the observable
private _items$ = new BehaviorSubject<T[]>([]);
get items$() {
return this._items$.asObservable();
}
I want something like this, which I found tbh without finding an item based on its id
public model: IModel;
this.theService.items$.pipe(
map(items => <IModel>items.find(item => item.ID === this.id))).subscribe(item => {
this.model = item
});
Any pointers would be appreciated.
CodePudding user response:
if you want to get the values from BehaviorSubject
directly - you need to call BehaviorSubject.value
.
so you can expose to method in your service:
private _items$ = new BehaviorSubject<T[]>([]);
get items$() {
return this._items$.asObservable();
}
get items() {
return this._items$.value;
}