2016-01-22 34 views
2

我正在用Typescript編寫一個簡單的Angularjs測試應用程序。我也爲了縮小目的而強制執行ng-strict-di。http攔截器上的Angularjs + strictdi錯誤(打字稿)

我創建了一個簡單的app.ts文件:

declare var angular: ng.IAngularStatic; 
module Application { 
"use strict"; 

export var app: ng.IModule = angular.module("Private", ["ngRoute"]); 

app.config(($httpProvider: ng.IHttpProvider) => { 
    $httpProvider.interceptors.push(Common.Services.HttpInterceptor.Factory); 
}); 

app.config(["$routeProvider", ($routeProvider: ng.route.IRouteProvider) => { 
     $routeProvider.when("/home", { templateUrl: "home.html" }); 
     $routeProvider.otherwise({ redirectTo: "/" }); 
    }]); 
} 

正如你所看到的,HTTP攔截正在推動

Common.Services.HttpInterceptor.Factory 

它是由如:

module Common.Services { 
    export class HttpInterceptor { 
     public static Factory($http: ng.IHttpService) { 
      return new HttpInterceptor($http); 
     } 

     static $inject = ["$http"]; 
      constructor($http: ng.IHttpService) { 
     } 

     public response = ((response) => { return response }) 

     public responseError = ((rejection) => { 
      return rejection; 
     }); 
    } 
} 

簡單,如你所見。 但每次我打我得到這個錯誤的家時間:

Error: [$injector:strictdi] function($http) is not using explicit annotation and cannot be invoked in strict mode http://errors.angularjs.org/1.4.9/ $injector/strictdi?p0=function(%24http)

但我注入$ HTTP時

static $inject = ["$http"]; 

我不知道如何解決這個問題。

如果這可以是任何幫助,下面編譯JS:

var Application; 
(function (Application) { 
    "use strict"; 
    Application.app = angular.module("Private", ["ngRoute"]); 

    Application.app.config(["$httpProvider", function ($httpProvider) { 
     $httpProvider.interceptors.push(Common.Services.HttpInterceptor.Factory); 
    }]); 
    Application.app.config(["$routeProvider", function ($routeProvider) {    
      $routeProvider.when("/home", { templateUrl: "home.html" }); 
      $routeProvider.otherwise({ redirectTo: "/" }); 
     }]); 
})(Application || (Application = {})); 

var Common; 
(function (Common) { 
    var Services; 
    (function (Services) { 
     var HttpInterceptor = (function() { 
      function HttpInterceptor($http) { 
       this.response = (function (response) { return response; }); 
       this.responseError = (function (rejection) { 
        return rejection; 
       }); 
      } 
      HttpInterceptor.Factory = function ($http) { 
       return new HttpInterceptor($http); 
      }; 
      HttpInterceptor.$inject = ["$http"]; 
      return HttpInterceptor; 
     })(); 
     Services.HttpInterceptor = HttpInterceptor; 
    })(Services = Common.Services || (Common.Services = {})); 
})(Common || (Common = {})); 

感謝您的幫助! 五

回答

0

下面HttpInterceptor也注入工廠方法:

HttpInterceptor.Factory.$inject = ['$http'];