【问题标题】:Angularjs + strictdi error on http interceptor (typescript)http拦截器上的Angularjs + strictdi错误(打字稿)
【发布时间】:2016-04-29 03:52:42
【问题描述】:

我正在使用 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/$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 = {}));

感谢您的帮助! 五、

【问题讨论】:

    标签: javascript angularjs typescript


    【解决方案1】:

    下面HttpInterceptor也注入了Factory方法:

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

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多