1

我正在使用拦截器将 JWT 令牌添加到我的 http 调用中。我的代码像这样工作正常:

angular
    .module("jwtKickStart")
    .factory("authInterceptorService", authInterceptorService);

authInterceptorService.$inject = ["$injector", "$q", "$timeout"];
function authInterceptorService($injector, $q, $timeout) {

    var $state, loginModal, $http;

    //...

但是,如果我将其包装在 IIFE 中,则会收到 Unknown Provider 错误:

(function () {
"use strict";

angular
    .module("jwtKickStart")
    .factory("authInterceptorService", authInterceptorService);

authInterceptorService.$inject = ["$injector", "$q", "$timeout"];
function authInterceptorService($injector, $q, $timeout) {

    var $state, loginModal, $http;

    //...

我不能使用 IIFE 吗?

编辑:这里有更多细节

错误消息是这样的:http : //errors.angularjs.org/1.3.8/$injector/unpr?p0=authInterceptorServiceProvider%20%3C-%20authInterceptorService%20%3C-%20%24http%20%3C- %20%24templateFactory%20%3C-%20%24view%20%3C-%20%24state

源代码可在此处获得: https ://github.com/capesean/JWTKickStart/tree/master/JWTKickStart.APP/app

4

1 回答 1

0

尝试像这样注入:

(function () {
"use strict";

angular
    .module("jwtKickStart")
    .factory("authInterceptorService",["$injector","$q","$timeout" authInterceptorService]);

function authInterceptorService($injector, $q, $timeout) {
// your code
}
于 2015-09-11T10:57:25.900 回答