import {Component, OnInit} from '@angular/core';
import {Http, Headers, URLSearchParams, Response} from '@angular/http';
import 'rxjs/Rx';
import {GlobalVariable} from './../../app.config'
import {Observable} from 'rxjs/Observable';
@Component({
templateUrl: './template.html',
})
export class RegistriesComponent implements OnInit{
errorMessage: string;
data:Array<any>;
constructor(private http: Http) {}
private urlApi = GlobalVariable.BASE_API_URL;
ngOnInit() {
this.getHeroes()
.subscribe(
res => this.data = res,
error => this.errorMessage = <any>error,
)
}
getHeroes (): Observable<Array<any>> {
return this.http.get(this.urlApi + 'rest/nsi/routes')
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
}
<tr *ngFor="let item of data">
<td>{{item.number}}</td>
</tr>
private extractData(res: Response): Array<{"number": any}> {
return res.json().data || [];
}
import { Component, OnInit } from '@angular/core';
import { HeroService } from './service';
import 'rxjs/Rx';
@Component({
templateUrl: 'template.html',
providers: [ HeroService ]
})
export class RegistriesComponent implements OnInit {
errorMessage: string;
public data: Array<any> = [
{'name':'test1'}
]
constructor (private heroService: HeroService) {}
ngOnInit() {
this.data = [
{'name':'test2'}
]
this.getHeroes()
}
getHeroes() {
console.log(this.heroService.getHeroes().subscribe(
data => this.data = data,
error => this.errorMessage = <any>error))
}
}
Subscriber {closed: false, syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: false, isStopped: false…}