What is observable, observer and subscribe in angular?

Here is a simple visual to see the difference: As seen above … an Observable is a stream of events or data. They are often returned from Angular methods, such as the http.get and the myinputBox.valueChanges. Subscribing “kicks off” the observable stream. Without a subscribe (or an async pipe) the stream won’t start emitting values. … Read more

How to subscribe to event emitter once?

A call to subscribe returns an instance of Disposable, which has a method dispose. Or if you are using RxJS 5, dispose has been renamed to unsubscribe (thanks @EricMartinez). And from the RxJS docs: …when we’re no longer interested in receiving the data as it comes streaming in, we call dispose on our subscription. Store … Read more

How to convert an Observable to a ReplaySubject?

You can use just observable.subscribe(subject) if you want to pass all 3 types of notifications because a Subject already behaves like an observer. For example: let subject = new ReplaySubject(); subject.subscribe( val => console.log(val), undefined, () => console.log(‘completed’) ); Observable .interval(500) .take(5) .subscribe(subject); setTimeout(() => { subject.next(‘Hello’); }, 1000) See live demo: https://jsbin.com/bayewo/2/edit?js,console However this … Read more

Angular 2 router resolve with Observable

Don’t call subscribe() in your service and instead let the route subscribe. Change return this.searchService.searchFields().subscribe(fields => { to import ‘rxjs/add/operator/first’ // in imports return this.searchService.searchFields().map(fields => { … }).first(); This way an Observable is returned instead of a Subscription (which is returned by subscribe()). Currently the router waits for the observable to close. You can … Read more

How to dispatch an empty action?

The choosen answer does not work with rxjs6 any more. So here is another approach. Although I prefer filtering for most cases as described in an another answer, using flatMap can sometimes be handy, especially when you are doing complex stuff, too complicated for a filter function: import { Injectable } from ‘@angular/core’; import { … Read more

Observable.forkJoin() doesn’t execute

forkJoin() requires all source Observables to emit at least once and to complete. This following demo completes as expected: const source = forkJoin( from([1,2,3]), from([9,8,7,6]) ).subscribe( x => console.log(‘GOT:’, x), err => console.log(‘Error:’, err), () => console.log(‘Completed’) ); Live demo: https://stackblitz.com/edit/rxjs-urhkni GOT: 3,6 Completed Jan 2019: Updated for RxJS 6

Why would I use RxJS interval() or timer() polling instead of window.setInterval()?

Advantage of RxJS: Laziness You can create your Observables and until you call subscribe nothing is happening. Observable = pure function. This gives you more control, easier reasoning and allows for next point… Composability You can combine interval/timer with other operators creating custom logic very easily in unified way – for example you can map, … Read more

How to Stop observable.timer in Angular2

There’re are basically two ways: call unsubscribe() on the Subscription object returned from the subscribe() call . use an operator To just unsubscribe you could do it like this. ngOnInit() { this.subscription = timer(100, 100).subscribe(t => { this.setFormData(); }); } private setFormData() { … this.subscription.unsubscribe(); } Or you can use Subject to complete the Observable … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)