javascript - Angular JS TypeScript IHttpService inject custom header value -
i have project make successful http request from typescript (angular http service) code to web api controller , display list in grid. project using angular js 1.4.x , typescript successfully.
full project's github url. , typescript code calls server below.
module app { export class studentlistservice { private qservice: ng.iqservice; private httpservice: ng.ihttpservice; constructor($q: ng.iqservice, $http: ng.ihttpservice) { this.qservice = $q; this.httpservice = $http; } get(): ng.ipromise<object[]> { var self = this; var deffered = self.qservice.defer(); self.httpservice.get('/api/values').then((result: any): void => { if (result.status === 200) { deffered.resolve(result.data); } else { deffered.reject(result); } }, error => { deffered.reject(error); }); return deffered.promise; } } studentlistservice.$inject = ['$q', '$http']; angular.module('app').service('studentlistservice', studentlistservice); }
now, want add custom header request call. have tried many ways, typescript keep giving me build error. or work around highly appreciated.
as long using correct typing file angular should able add header part of config, second argument of type ng.irequestshortcutconfig
extension of ihttpproviderdefaults
has header
property.
get<t>(url: string, config?: irequestshortcutconfig): ihttppromise<t>;
also added simplified code.
export class studentlistservice { static $inject = ['$q', '$http']; constructor(private qservice: angular.iqservice, private httpservice: angular.ihttpservice) { } get(): angular.ipromise<object[]> { //example of config structure var config: angular.irequestshortcutconfig = { headers: { "someheader":"somevalue" } } //add config , return promise directly instead of creating deferred object. promises chainable return this.httpservice.get('/api/values', config) .then((result: any) => result.data); //if want catch use ".catch" instead of second argument "then" better practice error may happen inside code in block caught well. } }
Comments
Post a Comment