gpt4 book ai didi

javascript - 错误: [$injector:unpr] Unknown provider: AuthServiceProvider Angular Service

转载 作者:行者123 更新时间:2023-11-28 07:12:26 24 4
gpt4 key购买 nike

我有一个小型 AngularJS 项目,但由于某种原因,我的服务文件无法成功注入(inject)任何 Controller 。也许我犯了一个我忽略的小语法错误,但我尝试了几种变体:

服务:

(function (){

'use strict';

angular
.module('tooltipEditorFrontEndApp')
.factory('AuthService', AuthService);

AuthService.$inject = ['$window', 'Restangular'];

function AuthService($window, Restangular) {

var exports = {
setAuthHeader: setAuthHeader
};

function setAuthHeader() {
if ($window.sessionStorage.getItem('token')) {
var token = angular.fromJson($window.sessionStorage.getItem('token')).token;
Restangular.setDefaultHeaders({ 'Authorization': 'Token '+token });
}
}

return exports;

}

})();

Controller :

(function (){

'use strict';

angular
.module('tooltipEditorFrontEndApp')
.controller('MainController', MainController);

MainController.$inject = ['$scope', '$window', 'Restangular', 'AuthService'];

function MainController($scope, $window, Restangular, AuthService) {

var test;

AuthService.setAuthHeader();

test = Restangular.all('states');

test.getList().then(function(states) {
$scope.states = Restangular.stripRestangular(states);
});
}

})();

加载脚本:

<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script scr="scripts/services/auth.service.js"></script>
<script src="scripts/controllers/main.controller.js"></script>
<script src="scripts/controllers/login.controller.js"></script>
<script src="scripts/controllers/about.controller.js"></script>
<!-- endbuild -->

最佳答案

我卸载了 Angular 1.4.1 并将其重新安装到项目中,现在该服务可以正常工作。

关于javascript - 错误: [$injector:unpr] Unknown provider: AuthServiceProvider Angular Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31164268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com