MarioHofer MarioHofer - 8 days ago 4
TypeScript Question

Get current value of Subject.asObservable() inside Angular service

I want to write a simple toggle inside an Angular2 service.

Therefore I need the current value of a

I observe (see below).

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


export class SettingsService {

private _panelOpened = new Subject<boolean>();
panelOpened$ = this._panelOpened.asObservable();

togglePanel() {!this.panelOpened$);


How do I get the current value from _panelOpened/panelOpened$?



Seems you are looking for BehaviorSubject

private _panelOpened = new BehaviorSubject<boolean>(false);

If you subscribe you get the last value as first event.

togglePanel() {
  this.currentValue = !this.currentValue;;