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 = {}));
感谢您的帮助! 五