我有一个小型的AngularJS项目,但由于某些原因,我的服务文件无法成功注入任何控制器。也许我发现了一个我忽略的轻微语法错误,但我尝试了几种变体:
服务:
(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;
}
})();
控制器:
(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 -->
答案 0 :(得分:0)
我卸载并重新安装了Angular 1.4.1到项目中,现在该服务正常运行。