14

In AngularJS for sending a request I use the builtin $http service.

What shall I use for sending a request to a server in Angular? I can't find any doc that covers the subject.

5
  • You can still use $http. Is it not working? Commented Mar 31, 2015 at 5:49
  • I use AngularJS2 not 1.x. Do you have example how to use $http? Commented Mar 31, 2015 at 6:06
  • Are you using the alpha for Angular 2? If so it doesn't have an http service yet. They are working on designing it here: github.com/jeffbcross/http-design Commented Apr 1, 2015 at 14:44
  • Thank you. Yeah it is not implement yet. Commented Apr 2, 2015 at 11:56
  • You can use fetch api... Commented May 28, 2015 at 13:57

2 Answers 2

5

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'})
@View({
  directives: [NgFor],
  template: `
    <h1>people</h1>
    <ul class="people">
      <li *ng-for="#person of people">
        hello, {{person.name}}
      </li>
    </ul>
  `
})
export class HttpCmp {
  people: Object;
  constructor(http: Http) {
    http.get('./people.json').toRx().map(res => res.json()).subscribe(people => this.people = people);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Any idea why this doesn't work anymore? map is not a member of EventEmitter
Check the "Breaking Changes" section in the link I added in my edit. Basically, you just need to call .toRx() before calling .map now
3

Simple http get sample:

http.get('./friends.json').map((res: Response) => res.json()).subscribe(res => this.result = res);

I have a working sample here: http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.