I am here to continue the discussion around AngularJS 2.0. I already discussed about the basic concept of Reactive programming concept or observables in Angular 2.0. Now, in this article, we discuss about the observables concept with http module or form modules in Angular 2.0. In case, you did not have a look at the previous articles of this series, go through the links mentioned below.
- AngularJS 2.0 From Beginning Introduction of AngularJS 2.0 (Day 1)
- AngularJS 2.0 From Beginning Component (Day 2)
- AngularJS 2.0 From Beginning Data Binding (Day 3)
- AngularJS 2.0 From Beginning Input Data Binding (Day 4)
- AngularJS 2.0 From Beginning - Output Property Binding (Day 5)
- AngularJS 2.0 From Beginning - Attribute Directive (Day 6)
- AngularJS 2.0 From Beginning - Structural Directives (Day 7)
- AngularJs 2.0 From Beginning - Pipes (Day 8)
- AngularJS 2.0 From Beginning - Viewchild (Day 9)
- AngularJS 2.0 From Beginning - Dynamic Grid (Day 10)
- AngularJS 2.0 From Beginning - Service (Day 11)
- AngularJs 2.0 From Beginning - ngContent (Day 12)
- AngularJS 2.0 From Beginning - Route Part I (Day 13)
- AngularJs 2.0 From Beginning - Route Part 2 (Day 14)
- AngularJs 2.0 From Beginning - ngForm Part 1 (Day 15)
- AngularJs 2.0 From Beginning - ngForm Part 2 (Day 16)
- AngularJs 2.0 From Beginning - Http Module - Part 1 (Day 17)
- AngularJs 2.0 From Beginning - Http Module - Part 2 (Day 18)
- AngularJs 2.0 From Beginning - Observables - Part 1 (Day 19)
Observables vs Promises
Both Promises and Observables provide us with abstraction that helps us to deal with the asynchronous nature of our Applications. However, there are important differences between the two:
As seen in the example above, Observables can define both the setup and teardown aspects of asynchronous behavior.
Observables are cancellable.
Moreover, Observables can be retried, using one of the retry operators provided by the API, such as retry and retryWhen. On the other hand, Promises require the caller to have an access to the original function, which returned the Promise in order to have a retry capability.
Observable HTTP Events
A common operation in any Web Application is getting or posting the data to a Server. Angular Applications do this with Http library, which previously used Promises to operate in an asynchronous manner. The updated Http library now incorporates Observables to trigger the events and getting new data.Observables Array Operations
In addition to simply iterating over an asynchronous collection, we can perform other operations such as filter or map and many more as defined in the RxJS API. This is what bridges an Observable with the iterable pattern and lets us conceptualize them as collections.
Here are two really useful array operations - map and filter. What exactly do these do?
map will create a new array with the results of calling a provided function on every element in this array. In the example given below, we used it to create a new result set by iterating through each item and appending the word ‘Mr’ abbreviation in front of every employee's name.
Note the chaining function style and the optional static typing, which comes with TypeScript, which we used in this example. Most importantly functions like filter return an Observable, as in Observables we get other Observables, which are similar to Promises. In order to use map and filter in a chaining sequence, we have flattened the results of our Observable, using flatMap. Since filter accepts an Observable and not an array, we have to convert our array of JSON objects from data.json() to an Observablestream. This is done with flatMap
To demonstrate the above concept, create the files given below with the code.
- import { Component, OnInit, ViewChild } from '@angular/core';
- import { Http, Response } from '@angular/http';
- import 'rxjs/Rx';
- @Component({
- moduleId: module.id,
- selector: 'home-page',
- templateUrl: 'app.component.homepage.html'
- })
- export class HomePageComponent implements OnInit {
- private data: Array<any> = [];
- constructor(private http: Http) {
- }
- ngOnInit(): void {
- }
- private loadData(): void {
- this.data = [];
- let self = this;
- this.http.request('http://localhost:5201/employee/getemployee')
- .flatMap((result) => result.json())
- .subscribe((result) => {
- self.data.push(result);
- });
- }
- private loadDataMap(): void {
- this.data = [];
- let self = this;
- this.http.request('http://localhost:5201/employee/getemployee')
- .flatMap((result) => result.json())
- .map((emp) => {
- emp["Name"] = "Mr. " + emp["Name"];
- return emp;
- })
- .subscribe((result) => {
- self.data.push(result);
- });
- }
- private loadDataFilter(): void {
- this.data = [];
- let self = this;
- this.http.request('http://localhost:5201/employee/getemployee')
- .flatMap((result) => result.json())
- .filter((emp) => emp["Salary"] <= 20000)
- .subscribe((result) => {
- self.data.push(result);
- });
- }
- }

Vinay Kumar GuptaPosted Jun 4, 2017, 12:06 AM
Great article this help a lot.