-
-
Save paritosh64ce/917e70f43e7d160a27b00f36491b87c0 to your computer and use it in GitHub Desktop.
Example - Angular 2+ controller calling data service which in turn calls custom common HTTP service
This file contains 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 { Http, Headers, RequestOptions } from '@angular/http'; | |
import { AsyncSubject } from 'rxjs/AsyncSubject'; | |
@Injectable() | |
export class myHTTPSvc { | |
constructor(private http: Http, private upSvc: MyUserProfileService) {} | |
get(url) : Observable<any> { | |
let subjectResult = new AsyncSubject(); | |
this.upSvc.getLoggedInUserDetails() | |
.subscribe(details => { | |
let headerConfig = this.getHeaderConfig(details); | |
// here is the actual GET call you want to make | |
this.http.get(url, headerConfig) | |
.map(data => data.json()) | |
.subscribe(result => { | |
// similar to deferred.resolve(result) we used to do with Angular 1.x | |
subjectResult.next(result); | |
subjectResult.complete(); | |
}); | |
}); | |
return subjectResult; | |
} | |
private getHeaderConfig(loggedInUserDetails: any): RequestOptions { | |
let options = new RequestOptions(); | |
options.headers = new Headers(); | |
options.headers.append('license', loggedInUserDetails.licenseInfo); | |
// or any details you want to push in message header | |
return options; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment