我试图在我的控制器中调用isAuthenticated方法,但它告诉我不是一个函数。下面是我的代码片段。
控制器
(function() {
'use strict';
angular
.module('app')
.controller('NavController', NavController);
NavController.$inject = ['USER_ROLES','AUTH_EVENTS','authService','$http'];
/* @ngInject */
function NavController(authService) {
var vm = this;
vm.name = '';
activate();
////////////////
function activate() {
authService.isAuthenticated().then(function(response){
vm.isLoggedin=response;
});
}
}})();
并在app.js(主模块)中包含所有依赖项
angular
.module('app', ['admin','app.router','app.auth','app.constants','user'])
authService位于app.auth.js
(function() {
'use strict';
angular
.module('app.auth',['LocalStorageModule','app.constants'])
.factory('authService', authService);
authService.$inject = ['$http','localStorageService','USER_ROLES'];
/* @ngInject */
function authService($http,localStorageService,USER_ROLES) {
var service = {
isAuthenticated: isAuthenticated
};
return service;
////////////////
function isAuthenticated(){
return $http({
method: 'GET',
url: '/api/v1/isAuthenticated',
headers: {
'Authorization': 'Bearer '+localStorageService.get('token')
}
}).then(function successCallback(response) {
return true;
}, function errorCallback(response) {
return false;
});
}
}})();
有谁知道我在这里做错了什么?需要帮助
答案 0 :(得分:1)
看起来你正在注射多种东西而且只宣告一种东西。它应该看起来像:
NavController.$inject = ['USER_ROLES','AUTH_EVENTS','authService','$http'];
/* @ngInject */
function NavController(USER_ROLES,AUTH_EVENTS,authService,$http) {