Answer the question
In order to leave comments, you need to log in
How to Angular2 service inject Http&?
Created a simple service in Angular2.
I can't figure out how to inject Http into it.
import {Injectable} from 'angular2/core';
import {Http, Headers, RequestOptions} from 'angular2/http';
import 'rxjs/Rx';
@Injectable()
export class Test{
constructor(_http: Http) {
this._http = _http;
}
getData = function() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://127.0.0.1:8085/', '{blabla}', options)
.map(DATA => DATA.json());
}
}
import {Component} from 'angular2/core';
import {Http, HTTP_PROVIDERS, Headers, RequestOptions} from 'angular2/http';
import 'rxjs/Rx';
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html',
styleUrls: ['app/app.component.css'],
viewProviders: [HTTP_PROVIDERS],
})
export class AppComponent{
constructor(http: Http) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
http.post('http://127.0.0.1:8085/', '{blabla}', options)
.map(DATA => DATA.json()).subscribe(DATA => {
this.Test = DATA.data;
}
}
}
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