Evgeniy Evgeniy - 1 year ago 150
Javascript Question

AngularJS 2 alternative $http

In AngularJS 1.x for sending request I use $http service.
What shall I use for sending request to server in AngularJS 2?
I can't find any doc about that.

Answer Source

EDIT: There is an api preview for the new http service on the Angular 2 website now

There is a basic http service in Angular 2 currently, but its very minimalist right now. This software is in alpha and is very likely to change, so you may just want to use the fetch API, implement your own XMLHttpRequest, or use a library like jQuery instead. Currently, the Angular 2 http api is essentially identical to the fetch API. Given that fetch is far less likely to change, I suggest just using that.

If you want to use the Angular 2 service, here is an example...

import {bootstrap, Component, View, NgFor, Inject} from 'angular2/angular2';
import {Http, httpInjectables} from 'angular2/http';

@Component({selector: 'http-app'})
  directives: [NgFor],
  template: `
    <ul class="people">
      <li *ng-for="#person of people">
        hello, {{person.name}}
export class HttpCmp {
  people: Object;
  constructor(http: Http) {
    http.get('./people.json').toRx().map(res => res.json()).subscribe(people => this.people = people);