http拦截器(typescript)上的Angularjs + strictdi错误

时间:2016-01-22 14:02:14

标签: javascript angularjs typescript

我正在使用Typescript编写一个简单的Angularjs测试应用程序。我也强制执行ng-strict-di用于缩小目的。

我创建了一个简单的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;
        });
    }
}

如你所见,简单。 但每次我到家时我都会遇到这个错误:

  

错误:[$ injector:strictdi]函数($ http)未使用显式注释,无法在严格模式下调用   http://errors.angularjs.org/1.4.9/ $注射器/ strictdi?P0 =函数(%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 = {}));

感谢您的帮助! 诉

1 个答案:

答案 0 :(得分:2)

下面的类render() { var elems = this.props.items.course_list; console.log(elems.course_list;); return ( <div> </div> ) } 也会注入Factory方法:

HttpInterceptor