Answer the question
In order to leave comments, you need to log in
What is wrong with http request in angular2?
I did everything according to the instructions from angular.io
app.service.ts:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class AppService {
constructor (private http: Http) {}
private dataUrl = 'http://jsonplaceholder.typicode.com/users';
private extractData(res: Response) {
let body = res.json();
return body || {};
}
getData (): Observable<any> {
return this.http.get(this.dataUrl)
.map(this.extractData)
}
}
import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';
import '../../public/css/styles.css';
@Component({
selector: 'my-app',
providers: [AppService],
template: require('./app.component.html'),
styles: [require('./app.component.css')],
})
export class AppComponent implements OnInit {
private users:any;
constructor(private appService: AppService) {}
ngOnInit() {
this.appService.getData().subscribe(res => this.users = res);
console.log(this.users);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question