import { Component } from '@angular/core'; import { Http } from '@angular/http'; @Component({ selector: 'my-app', template: `{{ responseText }}` }) export class AppComponent { responseText: string; constructor(private http: Http) { this.http.get('http://example.com').subscribe(response => { this.responseText = response.text(); }); } }
import { Component } from '@angular/core'; import { Http, Response } from '@angular/http'; @Component({ selector: 'my-app', template: `In this example, we use the Response object returned from an HTTP GET request to log the response data and also handle any errors that might occur in the HTTP request. The @angular/http library is included as part of the @angular/core package in Angular.{{ errorMessage }}` }) export class AppComponent { errorMessage: string; constructor(private http: Http) { this.http.get('http://example.com').subscribe( response => console.log(response), error => this.errorMessage =error ); } }