IIFE打破拦截器 - 未知的提供者

时间:2015-01-12 13:18:43

标签: angularjs angularjs-injector

我正在使用拦截器将JWT令牌添加到我的http调用中。我的代码工作得很好:

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

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

    var $state, loginModal, $http;

    //...

但是如果我将它包装在IIFE中,那么我会收到一个未知的提供程序错误:

(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%3 C-%20%24state

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

1 个答案:

答案 0 :(得分:0)

尝试像这样注入:

(function () {
"use strict";

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

function authInterceptorService($injector, $q, $timeout) {
// your code
}