Questions tagged behaviorsubject

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

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 […]

Getting an empty array on subscribing an observable

I am getting an empty array in component upon subscribing an object from service. I have used Subject behavior and trying to pass the data from subject to component but returning an empty array on component. Service:- allPassedData: BehaviorSubject<any> = new BehaviorSubject<any>([]); searchDepartments() { const fetchData3 = this.db.todos.orderBy(‘department’).keys((departments) => { alert("Departments are: " + departments.join(‘,’)); […]

By NoobCoder
Categorized as angular, behaviorsubject, rxjs Tagged , ,
1 9

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