Created
October 16, 2016 03:12
-
-
Save murac/0c59f6820df3078bb0c5b4c18ed3396c 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
| import {Injectable} from '@angular/core'; | |
| import {AuthHttp} from "angular2-jwt"; | |
| import {Headers} from "@angular/http"; | |
| import {Observable, Subject} from "rxjs"; | |
| import {User} from "./user"; | |
| @Injectable() | |
| export class UserService { | |
| private profile; | |
| constructor(private _authHttp: AuthHttp) { | |
| } | |
| get() { | |
| let myHeader = new Headers(); | |
| myHeader.append('Content-Type', 'application/json'); | |
| return this._authHttp.get('http://127.0.0.1:3001/api/profile/', {headers: myHeader}) | |
| .map(res => res.json()); | |
| } | |
| getProfile(): Observable<any> { | |
| console.log('getting profile'); | |
| var subject = new Subject<any>(); | |
| if (this.profile === undefined) { | |
| console.log('profile is undefined'); | |
| this.get().subscribe(profile=> { | |
| this.profile = profile; | |
| console.log('profile got', this.profile); | |
| subject.next(this.profile); | |
| }); | |
| } else { | |
| console.log('profile is here', this.profile); | |
| subject.next(this.profile); | |
| } | |
| return subject.asObservable(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment