2017-10-05 68 views
0

尝试添加一个简单的工厂,并收到错误:

Error: [$injector:unpr] http://errors.angularjs.org/1.4.6/$injector/unpr?p0=subfactoryProvider%20%3C-%20subfactory%20%3C-%20authService%20%3C-%20AuthorizationInterceptor%20%3C-%20%24http%20%3C-%20%24templateRequest%20%3C-%20%24compile 

我添加了一个脚本标签:

<script src="app/subfactory.js"></script> 

的subfactory被定义为如下:

(function() { 
    //'use strict'; 

    angular 
     .module('app') 
     .factory('subfactory', subfactory); 

    function subfactory() { 

     var subValue = {}; 
     return { 
      set: set, 
      get: get 
     }; 

     function get() { 
      return subValue; 
     } 

     function set(value) { 
      subValue = value; 
     } 
    } 

}); 

它在authService使用:

(function() { 
    //'use strict'; 

    angular 
     .module('app') 
     .factory('authService', authService); 

    authService.$inject = ['subfactory']; 

    function authService(subfactory) { 

     // removed code for brevity 

     mgr.getUser().then(function (user) { 
      if (user) { 
       var idToken = user.id_token; 
       var dataIdToken = getDataFromToken(idToken); 
       subfactory.set(dataIdToken.sub); 
      } else { 
       //console.log("User not logged in"); 
      } 
     }); 

我也有AuthorizationInterceptor调用authService:

app.factory("AuthorizationInterceptor", ['$q', '$injector', '$rootScope', '$window', 'authService', function ($q, $injector, $rootScope, $window, authService) { 

请,我怎样才能解决这个问题?

编辑:

<script src="app/app.js"></script> 
<script src="app/authService.js"></script> 
<script src="app/subfactory.js"></script> 

app.factory("AuthorizationInterceptor"...app.js

该错误消息被定义为在F12控制台的唯一消息。

+0

是什么,你的定义是那些工厂,这是什么',你所创建的'AuthorizationInterceptor app'变量的顺序'在? –

+0

如果你看到这个错误,这意味着或者subfactory.js没有被加载,或者在定义了'app'模块的文件之前被加载(并且应该有另一个说明这个错误的错误)。 – estus

回答

1

无法将其添加为评论。所以在这里添加一个答案。您需要更改文件声明的顺序。在你的情况下,错误是因为你在subfactory.js之前有authService.jsauthService.js搜索其依赖关系,但未找到它作为subfactory.js尚未添加。

<script src="app/app.js"></script>// If it main file whre angular module is created 
<script src="app/subfactory.js"></script> 
<script src="app/authService.js"></script> 
+0

现在我得到 - '错误:[$ injector:nomod] http://errors.angularjs.org/1.4.6/$injector/nomod?p0 = app' - 那么原始错误 –

+0

如果app.js是主文件whr角模块被定义为顶部 – Ved

2

尝试翻转你的宣言文件的顺序喜欢如下:

// only define your module here 
<script src="app/app.js"></script> 

// define your factories 
<script src="app/subfactory.js"></script> 
<script src="app/authService.js"></script> 

// define your authorization factory here just like the previous two 
<script src="app/Authorization.js"></script> 
+0

现在我得到 - 错误:[$ injector:nomod] http://errors.angularjs.org/1.4.6/$injector/nomod?p0 = app' - 然后原来的错误 –

+0

更新了我的答案。 –

+0

谢谢,你还请大拇指吗? –

相关问题