尝试添加一个简单的工厂,但得到错误:
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>该子工厂的定义如下:
(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");
}
});我还有调用authService的AuthorizationInterceptor:
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.js中定义了app.factory("AuthorizationInterceptor"...
该错误消息是F12控制台中的唯一消息。
发布于 2017-10-05 19:02:00
尝试颠倒声明文件的顺序,如下所示:
// 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>发布于 2017-10-05 19:01:13
无法将其添加为注释。所以在这里添加一个答案。您需要更改文件声明的顺序。在您的例子中,错误是因为您在subfactory.js之前使用了authService.js。authService.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>https://stackoverflow.com/questions/46583700
复制相似问题