Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I console.log the value of a observable?

I am using angular 2 and RxJS, and I am wondering how i can do the following:

In my component, I have defined the following:

count: Observable<number>;

In my component's constructor, I am doing the following:

constructor(     private store: Store<any>   ) {     this.count = this.store.select<any>(state => state.count);   } 

How can I view the current value for the count? Right now if I console.log(this.count) I get a big object to log. If I want to view just the value for this.count, how can I do that?

like image 524
user1354934 Avatar asked Aug 19 '16 16:08

user1354934


People also ask

How do you know if Observable has value?

The RxJS isEmpty() operator returns an observable with a Boolean value indicating whether the observable was empty or not. It returns an output as true if the source observable is empty; otherwise, false.

How do you get the last emitted value from Observable?

Rather than a standard subject (Observable) that just emits values as they come in, a BehaviorSubject emits the last value upon subscribe() . You can also get the last value manually using the BehaviorSubjects getValue() method.

What is Observable value?

An ObservableValue is an entity that wraps a value and allows to observe the value for changes. In general this interface should not be implemented directly but one of its sub-interfaces ( ObservableBooleanValue etc.). The value of the ObservableValue can be requested with getValue() .

Can you subscribe to an Observable?

SubscribinglinkAn Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications.


2 Answers

With a regular observable you only get the value when it changes, so if you want to console.log out the value you will need to console.log it in the subscription:

constructor(     private store: Store<any>   ) {     this.count = this.store.select<any>(state => state.count);     this.count.subscribe(res => console.log(res));   } 

However if you are wanting to be able to get the current value at any time what you will be wanting is a BehaviorSubject (which combines an Observable and an Observer in function...import it from the rxjs library like you do Observable).

private count:BehaviorSubject<number> = new BehaviorSubject<number>(0);  constructor(     private store: Store<any>   ) {     let self = this;     self.store.select<any>(state => self.count.next(state.count));   } 

Then any time you want to get the current value of the count you would call this.count.getValue() to change the value you would call this.count.next(<the value you want to pass in>). That should get you what you are looking for.

like image 83
Maarek Avatar answered Sep 22 '22 07:09

Maarek


With recent versions of RxJS (AFAIR starting from 6.0) the proper way is to use .pipe(). And to answer to your question you need tap operator.

constructor(     private store: Store<any>   ) {     this.count = this.store.select<any>(state => state.count).pipe(         tap(countValue => console.log('Count: ', countValue))     );   } 
like image 23
Vladimir Prudnikov Avatar answered Sep 21 '22 07:09

Vladimir Prudnikov