Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable.subscribe() works only once (ngrx\angular2)

I'm subscribing to a store property in a component constructor

constructor(private someService: SomeService){
        someService.someObservable.subscribe((data: Some)=> {
            this.some = data;
            console.log('changed');
        });
}

In the service, the constructor looks like this:

constructor(private store: Store<any>){
        this.someObservable = <Observable<{}>>store.select('some');
}

I keep changing the state of this object, but the logging only appear once. What's also weird is that I'm accessing the this.some property directly in my template (without async pipe), and it updates perfectly! It seems like the this.some property is being updated, but the next() in the subscription isn't working.

help anyone? thanks!

like image 517
Yonatan Avatar asked Jun 02 '16 19:06

Yonatan


People also ask

Can you subscribe to observable multiple times?

Just like calling a function multiple times gives you multiple return values. So if you subscribe multiple times to an an Observable returned by the Angular HTTP Client, that will result in multiple HTTP requests!

What does subscribing to an observable do?

Subscribing to an Observable is like calling a function, providing callbacks where the data will be delivered to. This is drastically different to event handler APIs like addEventListener / removeEventListener . With observable.subscribe , the given Observer is not registered as a listener in the Observable.

What is subscribe () in Angular?

Subscribe() is a method in Angular that connects the observer to observable events. Whenever any change is made in these observable, a code is executed and observes the results or changes using the subscribe method. Subscribe() is a method from the rxjs library, used internally by Angular.02-Jan-2022.

What is observable and subscribe in Angular?

Observables in Angular are generally declarative i.e., if you ought to define a function for value publication. It won't be executed until and unless it is subscribed. The subscriber is termed as a consumer who receives the notifications until the function completes itself or until they manually unsubscribe.


1 Answers

In my very similar case the problem was that one of the subscription handlers was throwing an error. When an error is encountered the observable stream stops emitting new values.

like image 186
pbalaga Avatar answered Oct 14 '22 05:10

pbalaga