Questions tagged behaviorsubject

Explore the latest questions and answers asked by our top developers.

How to subscript to a filtered observable

I want to subscript to an observable that has a filtered value, but the filtering part isn’t working. In my service, I have export class UserService { private userList: user[]; public userList$ = new BehaviorSubject<user[]>([]); constructor() { this.userList = [ { name : "ben", isActive : true, }, { name : "chen", isActive : true, […]

By Chen Long
Categorized as angular, behaviorsubject, observable, rxjs Tagged , , ,

Should I complete BehaviorSubject on the component destroy – Angular2

I have a service provided in a component, so it runs ngOnDestory if the component is going to be destroyed. Every subscription runs with takeUntil(this.destory$) (which emits on the component’s ngOnDestory hook) or with the async pipe, so everything is unsubscribed when the component destroys. The question is, if I have a BehaviorSubject inside the […]

By Romanov Eugene
Categorized as angular, behaviorsubject, rxjs, subject Tagged , , ,

Unwanted subscription on observable service

Following the cached service pattern I’m using a BehaviourSubject subscribed by using a read-only Observable created from it in various components // credential.service.ts private _credentialList$: BehaviorSubject<Credential[]> readonly credentialListObv$: Observable<Credential[]> … this. credentialListObv$ = this.credentialList$.asObservable() … getCredentialList(…): Observable<Credential[]> { return this.http.get<Credential[]>( “ ).pipe( tap(credList => { … this._credentialList$.next(credList) }), ) } So from the component I […]

Angular websokcet and behaviorSubject unit test

Getting error with websocket unit test Error: <spyOn> : webSocket is not declared writable or has no setter Usage: spyOn(<object>, <methodName>) here’s my service and component // tikcet.service.ts import { webSocket } from ‘rxjs/webSocket’; export class ticketService { ticketVar: number = 0; private ticketCtrl = new BehaviorSubject(this.ticketVar); setTicketSubject(value: number) { } getTicketSubject() { return […]

Type ‘void’ is not assignable to type ‘Observable<any>’ How can I fix this?

I’m trying to pass the return value of my API to my BehaviorSubject, but I’m encountering the Typescript error above. It’s not clear to me how to fix it. The line of code that is causing the error is return How should this be passed to my BehaviorSubject? export class SleepService { private endpoint […]

By London804
Categorized as angular, behaviorsubject, observable Tagged , ,

Can someone explain that why the BehaviorSubject result like this?

I wrote a service contain a BehaviorSubject called loginStatus and a function to return that. private loginStatus = new BehaviorSubject<boolean>(false); getLoginStatus(): Observable<boolean> { return this.loginStatus; } Then I subscribe it in other component’s DOM with a async pipe in 4 different div, I just commented the shareReplay to show what I’m wondering. loginStatus$ = this.authService.getLoginStatus().pipe( […]

By Carl Tin
Categorized as angular, behaviorsubject, rxjs Tagged , ,

Angular, consuming Observables with BehaviorSubject issues

I’ve created a mess of code trying to sort out the actual way to properly consume shared data. I have a service which is consuming data from other services, and returning a calculated interface. (IDriverProfile) The challenge I am facing, is that I want to consume all services at once. I originally used forkJoin for […]

Subject vs BehaviorSubject in Angular

I am passing data between 2 independent components using shared service. When I used a subject in my shared service, I could not see the subscribed data in html of the subscribed component but could see it in the console after subscribing. Whereas if I use Behaviorsubject, it worked fine. Can anyone explain me the […]

Behavior Subject Vs. Public?

We can use Behavior Subject to access a variable in more than 1 component, but I just discovered we can access variables in other components just by declaring them as "public" in the constructor of the files we wish to access them in. Is Behavior Subject the preferred way to pass variable values between components, […]

By cfoxy17
Categorized as angular, behaviorsubject, components, rxjs Tagged , , ,

BehaviorSubject emitter object is undefined

i have the below posted service and i am using ‘BehaviorSubject to send an event from the child-component to the parent-component as shown below. in the html of the chils component i have savebutton that when clicked the method save()should be called and the event should be broadcast and the logs in the subscripton this.subscriptionBroadcasrEmitterOnSaveButtonClicked […]

1 2 3 10

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us