Angular 当我从控制器引用时出现服务错误
Angular service error when I referenced from controller
我试着做了一个登录模块。我定义了一个 cotroller,我试图从服务访问函数,但我收到了一个 ReferenceError:
ReferenceError: Auth is not defined
控制器:
class NavbarController {
constructor($location) {
this.$location = $location;
this.isLoggedIn = Auth.isLoggedIn;
this.isAdmin = Auth.isAdmin;
this.getCurrentUser = Auth.getCurrentUser;
}
isActive(route) {
return route === this.$location.path();
}
}
angular.module('rideSharingApp')
.controller('NavbarController', NavbarController);
服务:
(function() {
function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
var safeCb = Util.safeCb;
var currentUser = {};
var userRoles = appConfig.userRoles || [];
if ($cookies.get('token') && $location.path() !== '/logout') {
currentUser = User.get();
}
var Auth = {
isLoggedIn: function(callback) {
if (arguments.length === 0) {
return currentUser.hasOwnProperty('role');
}
return Auth.getCurrentUser(null)
.then(function(user) {
var is = user.hasOwnProperty('role');
safeCb(callback)(is);
return is;
});
}
};
return Auth;
}
angular.module('rideSharingApp.auth')
.factory('Auth', AuthService);
})();
我不知道为什么会收到该错误。我不太了解angular,所以你能帮我解决这个错误吗?
谢谢。
您没有在“NavbarController”中传递“Auth”依赖...
在 Controller 的构造函数中传递 Auth...希望它能解决问题...
谢谢
我试着做了一个登录模块。我定义了一个 cotroller,我试图从服务访问函数,但我收到了一个 ReferenceError:
ReferenceError: Auth is not defined
控制器:
class NavbarController {
constructor($location) {
this.$location = $location;
this.isLoggedIn = Auth.isLoggedIn;
this.isAdmin = Auth.isAdmin;
this.getCurrentUser = Auth.getCurrentUser;
}
isActive(route) {
return route === this.$location.path();
}
}
angular.module('rideSharingApp')
.controller('NavbarController', NavbarController);
服务:
(function() {
function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
var safeCb = Util.safeCb;
var currentUser = {};
var userRoles = appConfig.userRoles || [];
if ($cookies.get('token') && $location.path() !== '/logout') {
currentUser = User.get();
}
var Auth = {
isLoggedIn: function(callback) {
if (arguments.length === 0) {
return currentUser.hasOwnProperty('role');
}
return Auth.getCurrentUser(null)
.then(function(user) {
var is = user.hasOwnProperty('role');
safeCb(callback)(is);
return is;
});
}
};
return Auth;
}
angular.module('rideSharingApp.auth')
.factory('Auth', AuthService);
})();
我不知道为什么会收到该错误。我不太了解angular,所以你能帮我解决这个错误吗?
谢谢。
您没有在“NavbarController”中传递“Auth”依赖... 在 Controller 的构造函数中传递 Auth...希望它能解决问题...
谢谢