Develop @MattBurnell in the comments on the accepted answer;
If you just need the current value right now (and you don't want a lot of subscribers floating around), you can simply use the getValue () method for BehaviorSubject.
import {Component, OnInit} from 'angular2/core'; import {BehaviorSubject} from 'rxjs/subject/BehaviorSubject'; @Component({ selector: 'bs-test', template: '<p>Behaviour subject test</p>' }) export class BsTest implements OnInit { private _panelOpened = new BehaviorSubject<boolean>(false); private _subscription; ngOnInit() { console.log('initial value of _panelOpened', this._panelOpened.getValue()); this._subscription = this._panelOpened.subscribe(next => { console.log('subscribing to it will work:', next); });
This will lead to:
initial value of _panelOpened false subscribing to it will work: false ==== _panelOpened is now true ==== subscribing to it will work: true getValue will get the next value: true
See plunker :
source share