Last active
November 15, 2016 01:23
-
-
Save jrwebdev/00795097c28df51386f6b9fa6ca0e817 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Angular 1 | |
| const module = angular.module('myModule', []); | |
| module.service('UserService', ['$http', function ($http) { | |
| this.getUsers = () => { | |
| return $http.get('http://api.mywebsite.com/users') | |
| .then(res => res.data) | |
| .catch(res => new Error(res.data.error)); | |
| } | |
| }]); | |
| /***************************************************************/ | |
| // Angular 2 | |
| import { Injectable } from '@angular/core'; | |
| import { Http, Response } from '@angular/http'; | |
| import { Observable } from 'rxjs/Observable'; | |
| @Injectable() | |
| class UserService { | |
| constructor(private http: Http) {} | |
| getUsers(): Observable<User[]> { | |
| return this.http.get('http://api.mywebsite.com/users') | |
| .map((res: Response) => res.json()) | |
| .catch((res: Response) => Observable.throw(res.json().error); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment