Questions tagged [behaviorsubject]

Use this tag for questions related to a BehaviorSubject, which represents a value that changes over time.

601 questions
1010
votes
14 answers

What is the difference between BehaviorSubject and Observable?

I'm looking into the design patterns of RxJS, and I do not understand the difference between BehaviorSubject and Observable. From my understanding, BehaviorSubject can contain a value that may change. It can be subscribed to and subscribers can…
Kevin Mark
  • 10,173
  • 3
  • 10
  • 10
434
votes
10 answers

What is the difference between Subject and BehaviorSubject?

I'm not clear on the difference between a Subject and a BehaviorSubject. Is it just that a BehaviorSubject has the getValue() function?
Mike Jerred
  • 9,551
  • 5
  • 22
  • 42
71
votes
4 answers

How to convert an Observable into a BehaviorSubject?

I'm trying to convert an Observable into a BehaviorSubject. Like this: a$ = new Observable() b$ = BehaviorSubject.create(new BehaviorSubject(123), a$) // I have also tried: a$ = new Observable() b$ = new BehaviorSubject(a$, 123) // And: a$ = new…
awmleer
  • 1,658
  • 3
  • 13
  • 28
68
votes
2 answers

Simple way to get the current value of a BehaviorSubject with rxjs5

Previously in rxjs4 there was a method in the BehaviorSubject called: getValue() (doc here). This method does not exist anymore in rxjs5. So the only solution that I found to get the value of a BehaviorSubject was: let…
Clement
  • 3,860
  • 4
  • 24
  • 36
26
votes
4 answers

How to implement behavior subject using service in Angular 8

I'm new to Angular and I'm having an issue. I'm creating an app with several sibling components. When I update a value in one component other components don't update. I know that to resolve this issue I should use behaviour subject. But how do I…
kkD97
  • 263
  • 1
  • 3
  • 5
20
votes
1 answer

RXJS BehaviorSubject getValue vs value

I was wondering what the main difference is between the getValue function and the readonly value property on the BehaviorSubject? Is there a benefit of using one over the the other?
20
votes
2 answers

Angular 2 rxjs observables created from BehaviorSubject are not working with forkJoin

I am trying to use Observable.forkJoin and the subscribe handler is never getting hit. The forkJoin operator is working for me in other parts of my app and the only difference I can think of in the non-working scenario is that the observables are…
cboston
  • 482
  • 4
  • 10
14
votes
1 answer

Why does piping a BehaviorSubject create an AnonymousSubject in RxJS?

When creating an RxJS BehaviorSubject, it stays a BehaviorSubject until it's pipe'd. As soon a pipe'd version is returned, it becomes an AnonymousSubject. Examples: // Instance of `BehaviorSubject` const behaviorSubject$ = new BehaviorSubject({…
Kevin Ghadyani
  • 6,829
  • 6
  • 44
  • 62
12
votes
1 answer

Should I use asObservable in BehaviorSubject?

I am wondering what is the two approach in the following code in BehaviorSubject. As far as I know: The asObservable method not only cast it to an Observable, it also removes the Observer implementation. Therefore you are not able to call next,…
Jack
  • 1
  • 21
  • 118
  • 236
11
votes
2 answers

RxSwift: What is the usage difference between BehaviorSubject and BehaviorRelay?

I am aware that BehaviorRelay is replacing Variable, and both BehaviorSubject and BehaviorRelay starts with an initial value, and replays it or the latest value to the subscribers. What are the differences then? in which case you would use one over…
Alan Steiman
  • 412
  • 1
  • 4
  • 14
11
votes
3 answers

RxJS share() operator with BehaviorSubject and async pipe - Angular

I have a BehaviorSubject that is being consumed as an observable: testForStack$: Observable; ngOnInit(){ const bs = new BehaviorSubject(true); this.testForStack$ = bs .asObservable() .do(t => console.log('subscribed')) …
Tony Scialo
  • 5,457
  • 11
  • 35
  • 52
11
votes
2 answers

Angular data loss on reload using BehaviorSubject

I'm using a data service to send the user data to the app and display the username in my header component. If I sign in in my app with login component the username is correctly displayed, but when I reload the page the user data disappears. on login…
ufollettu
  • 822
  • 3
  • 19
  • 45
11
votes
1 answer

How to combine multiple rxjs BehaviourSubjects

I'm building out an Angular2 app, and have two BehaviourSubjects that I want to logically combine into one subscription. I'm making two http requests and want to fire an event when both of them come back. I'm looking at forkJoin vs combineLatest. …
cobolstinks
  • 6,801
  • 16
  • 68
  • 97
10
votes
1 answer

How to signal a BehaviorSubject that the stream is completed

In angular 2, mySubject (see code) compiles a complete() function, but it errors during execution saying there is no such function. I was unable to get onComplete() to compile. import { Component, OnInit } from '@angular/core'; import {…
Yogi Bear
  • 943
  • 2
  • 16
  • 32
9
votes
2 answers

How can I set a new BehaviorSubject value without calling next?

I have a need to set the value of my BehaviorSubject without triggering a next call to any subscriptions. I tried doing this: this.mySubject = new BehaviorSubject(newVal); but that removes all of the subscriptions as well. this.mySubject.value =…
Scottie
  • 11,050
  • 19
  • 68
  • 109
1
2 3
40 41