How to get current value of RxJS Subject or Observable

0 votes

I have an Angular service:

import {Storage} from './storage';
import {Injectable} from 'angular2/core';
import {Subject}    from 'rxjs/Subject';

@Injectable()
export class SessionStorage extends Storage {
  private _isLoggedInSource = new Subject<boolean>();
  isLoggedIn = this._isLoggedInSource.asObservable();
  constructor() {
    super('session');
  }
  setIsLoggedIn(value: boolean) {
    this.setItem('_isLoggedIn', value, () => {
      this._isLoggedInSource.next(value);
    });
  }
}

Everything works great. But I have another component which doesn't need to subscribe, it just needs to get the current value of isLoggedIn at a certain point in time. How can I do this?

Sep 8, 2020 in Angular by kartik
• 37,510 points
16,136 views

1 answer to this question.

0 votes

Hello @kartik,

A Subject or Observable doesn't have a current value. When a value is emitted, it is passed to subscribers and the Observable is done with it.

If you want to have a current value, use BehaviorSubject which is designed for exactly that purpose. BehaviorSubject keeps the last emitted value and emits it immediately to new subscribers.

It also has a method getValue() to get the current value.

To know more about Angular, It's recommended to join Angular Course today.

Hope it helps!!!
Thank you!!

answered Sep 8, 2020 by Niroj
• 82,880 points

Related Questions In Angular

0 votes
1 answer

How to allow download of .json file with ASP.NET?

Hello @kartik, If you want to manually add ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,880 points
1,861 views
0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,880 points
579 views
0 votes
1 answer

How to Stop observable.timer in Angular?

Hello @kartik, There're are basically two ways: call unsubscribe() on the ...READ MORE

answered Sep 8, 2020 in Angular by Niroj
• 82,880 points
13,591 views
0 votes
1 answer

How to synchronise Angular http get?

Hello @kartik,  the only way around this: wrap ...READ MORE

answered Sep 8, 2020 in Angular by Niroj
• 82,880 points
8,360 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,880 points
2,734 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28, 2020 in Java-Script by Niroj
• 82,880 points
17,447 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
2,947 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
999 views
0 votes
1 answer

How to get parameter value from query string?

Hello @kartik, Using component <Route path="/users/:id" component={UserPage}/> this.props.match.params.id The component is ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,880 points
1,428 views
0 votes
1 answer

How can we go back to previous page after having some error on request made through current page ?

$route is used for deep-linking URLs to controllers ...READ MORE

answered Feb 11, 2020 in Angular by Niroj
• 82,880 points
988 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP