Questions tagged [observable]

An observable is typically a programming construct that can be "watched" by other parts of the code, called the "observers". Different frameworks and programming languages have different implementations for observables, so this tag should typically be used in conjunction with others.

An observable is typically a programming construct that can be "watched" by other parts of the code, called the "observers". Different frameworks and programming languages have different implementations for observables, so this tag should typically be used in conjunction with others.

An observer may be any object that implements interface Observer. An observable object can have one or more observers. After an observable instance changes, an application calling the Observable's “notification” method causes all of its observers to be notified of the change by a call to their update method.

References

8334 questions
821
votes
25 answers

Angular/RxJs When should I unsubscribe from `Subscription`

When should I store the Subscription instances and invoke unsubscribe() during the NgOnDestroy life cycle and when can I simply ignore them? Saving all subscriptions introduces a lot of mess into component code. HTTP Client Guide ignore…
264
votes
7 answers

Delegation: EventEmitter or Observable in Angular

I am trying to implement something like a delegation pattern in Angular. When the user clicks on a nav-item, I would like to call a function which then emits an event which should in turn be handled by some other component listening for the…
the_critic
  • 12,014
  • 19
  • 60
  • 109
206
votes
11 answers

Return an empty Observable

The function more() is supposed to return an Observable from a get request export class Collection { public more = (): Observable => { if (this.hasMore()) { return this.fetch(); } else { // return empty observable …
Murhaf Sousli
  • 10,769
  • 18
  • 103
  • 170
204
votes
10 answers

When should we use Observer and Observable?

An interviewer asked me: What is Observer and Observable and when should we use them? I wasn't aware of these terms, so when I got back home and started Googling about Observer and Observable, I found some points from different resources: 1)…
Ravi
  • 28,657
  • 41
  • 110
  • 158
150
votes
7 answers

Difference between Java 8 streams and RxJava observables

Are Java 8 streams similar to RxJava observables? Java 8 stream definition: Classes in the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements.
rahulrv
  • 5,424
  • 6
  • 30
  • 54
144
votes
6 answers

How to create an Observable from static data similar to http one in Angular?

I am having a service that has this method: export class TestModelService { public testModel: TestModel; constructor( @Inject(Http) public http: Http) { } public fetchModel(uuid: string = undefined): Observable { …
Michail Michailidis
  • 10,050
  • 6
  • 44
  • 85
140
votes
5 answers

How to catch exception correctly from http.request()?

Part of my code: import {Injectable} from 'angular2/core'; import {Http, Headers, Request, Response} from 'angular2/http'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/map'; @Injectable() export class myClass { …
Nick
  • 8,381
  • 6
  • 50
  • 75
139
votes
6 answers

Creating and returning Observable from Angular 2 Service

This is more of a "best practices" question. There are three players: a Component, a Service and a Model. The Component is calling the Service to get data from a database. The Service is using: this.people = http.get('api/people.json').map(res =>…
Joseph Genchik
  • 1,631
  • 3
  • 12
  • 17
126
votes
7 answers

How to check the length of an Observable array

In my Angular 2 component I have an Observable array list$: Observable; In my Template I have
No records found.
118
votes
5 answers

Angular 4+ ngOnDestroy() in service - destroy observable

In an angular application we have ngOnDestroy() lifecycle hook for a component / directive and we use this hook to unsubscribe the observables. I want to clear / destory observable that are created in an @injectable() service. I saw some posts…
mperle
  • 2,514
  • 7
  • 12
  • 31
117
votes
9 answers

How do I return the response from an Observable/http/async call in angular?

I have service which returns an observable which does an http request to my server and gets the data. I want to use this data but I always end up getting undefined. What's the problem? Service: @Injectable() export class EventService { …
eko
  • 34,608
  • 9
  • 60
  • 85
106
votes
4 answers

Using an array from Observable Object with ngFor and Async Pipe Angular 2

I am trying to understand how to use Observables in Angular 2. I have this service: import {Injectable, EventEmitter, ViewChild} from '@angular/core'; import {Observable} from "rxjs/Observable"; import {Subject} from "rxjs/Subject"; import…
C. Kearns
  • 1,371
  • 3
  • 11
  • 17
102
votes
1 answer

Chaining RxJS Observables from http data in Angular2 with TypeScript

I'm currently trying to teach myself Angular2 and TypeScript after happily working with AngularJS 1.* for the last 4 years! I have to admit I am hating it but I am sure my eureka moment is just around the corner... anyway, I have written a service…
Mike Sav
  • 13,017
  • 24
  • 89
  • 121
101
votes
5 answers

How can I create an observable with a delay

Question For testing purposes, I'm creating Observable objects that replace the observable that would be returned by an actual http call with Http. My observable is created with the following code: fakeObservable = Observable.create(obs => { …
Adrien Brunelat
  • 3,784
  • 4
  • 27
  • 41
101
votes
10 answers

How to make one Observable sequence wait for another to complete before emitting?

Say I have an Observable, like so: var one = someObservable.take(1); one.subscribe(function(){ /* do something */ }); Then, I have a second Observable: var two = someOtherObservable.take(1); Now, I want to subscribe() to two, but I want to make…
Stephen
  • 18,089
  • 8
  • 57
  • 97
1
2 3
99 100