小编Max*_*kiy的帖子

AngularJS中的TypeScript拦截器

我在使用TypeScript在AngularJS中设置请求拦截器时遇到问题

以下代码段工作,不工作变体已注释掉.无论我在构造函数中注入什么,request方法中的局部变量都是未定义的.

module Services
{
    export class AuthInterceptor
    {
        public static Factory(TokenService: Services.ITokenService)
        {
            return new AuthInterceptor(TokenService);
        }

        constructor(private TokenService: Services.ITokenService)
        {
            this.request = (config: ng.IRequestConfig) =>
            {
                config.headers = config.headers || {};
                if(this.TokenService.IsAuthorised())
                    config.headers.Authorization = 'Bearer ' + this.TokenService.Token;
                return config;
            };
        }

        public request: (config: ng.IRequestConfig)=>ng.IRequestConfig;

/* THIS IS NOT WORKING

        public request(config)
        {
                    // this.TokenService is undefined here as well as $window or $q which I tried to inject
            config.headers = config.headers || {};
            if(this.TokenService.Token …
Run Code Online (Sandbox Code Playgroud)

this angularjs typescript

13
推荐指数
1
解决办法
7122
查看次数

标签 统计

angularjs ×1

this ×1

typescript ×1