Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created March 17, 2016 02:22
Show Gist options
  • Save johnlindquist/5b8a1111cda507ea3d05 to your computer and use it in GitHub Desktop.
Save johnlindquist/5b8a1111cda507ea3d05 to your computer and use it in GitHub Desktop.
import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import 'rxjs/Rx';
import {HTTP_PROVIDERS, Http} from 'angular2/http';
@Component({
selector: 'app',
template: `
<ul *ngFor="#person of people | async">
{{person.name}}
</ul>
`
})
export class App {
people = this.http
.get('http://swapi.co/api/people')
.map(res => res.json().results);
constructor(public http:Http){}
}
bootstrap(App, [HTTP_PROVIDERS]);
@basarat
Copy link

basarat commented Mar 17, 2016

Prefer:

export class App {
    people: Rx.Obervable<any>;
    constructor(public http:Http) {
        this.people = this.http
                .get('http://swapi.co/api/people')
                .map(res => res.json().results);
    }
}

Reason: Its clear to see from the constructor any significant initialization. This is just the way I've done it in the past when $http.get would actually send an XHR.

PS: It this was React I would be looking at componentDidMount : https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount (mentions XHRs should happen here) < This might be a completely useless piece of information but explains where I am coming from 🌹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment