TypeScript interceptor in AngularJS -
I use typescript having trouble setting up a request interceptor in AngularJS
Following The snippet works, the working version is commented out. Anything that I do not inject into the constructor is undefined in the local variable wrong To understand why you need it: request method.
module service {export category AuthInterceptor {public static factory (Token Service: Services. ITokenService) {return new AuthInterceptor (Toknsrvisej); } Constructor (Private token service: Services. Information service) {this.request = (config: ng.IRequestConfig) = & gt; {Config.headers = config.headers || {}; If (this .tokenService.IsAuthorised ()) config.headers.Authorization = 'beerer' + this.TokenService.Token; Return configuration; }; } Public Request: (config: ng.IRequestConfig) = & gt; NG: Irvevest Confief; / * This public request (config) {// it is not working. TokenService is undefined here, as well as $$ or $ q, which I tried to inject config.headers = config.headers || {}; If (this. TokenService.Token! = "") Config.headers.Authorization = 'beerer' + this.TokenService.Token; Return configuration; } *}} Angular.module ("Services"). Config (($ httpProvider: ng.IHttpProvider) = & gt; {$ httpProvider.interceptors.push (Services.AuthInterceptor.Factory);});
This is due to Solution:
public request = (config) = & gt; {// This TokenService is undetermined here, as well as $$ or $ q, which I tried to inject config.headers = config.headers || {}; If (this. TokenService.Token! = "") Config.headers.Authorization = 'beerer' + this.TokenService.Token; Return configuration; }
Comments
Post a Comment