注入控制器时,服务未定义
Service undefined when injected into controller
我有一个控制器,我已经注入了一个工厂,但是当我调用该工厂的方法时,它返回为未定义。不知道我做错了什么。如有任何帮助,不胜感激。
工厂:(function(){
'use strict';
// Declare factory and add it 'HomeAutomation' namespace.
angular.module('HomeAutomation').factory('AuthenticationService', ['$http','$localStorage', '$window', function($http, $localStorage, $window){
var service = {};
service.login = Login;
service.logout = Logout;
service.parseJWT = parseJWT;
service.loginStatus = loginStatus;
return service;
function Login(email, password, callback){
$http.post('api/user/login', {email: email, password: password})
.success(function(res){
// Login successful if there is a token in the response.
if(res.token){
// store username and token in local storage to keep user logged in between page refreshes
$localStorage.currentUser = { email: email, token: res.token };
// add jwt token to auth header for all requests made by the $http service
$http.defaults.headers.common.Authorization = 'Bearer ' + res.token;
callback(true);
}else{
callback(res);
}
}).error(function(err){
console.log(err);
});
}
function Logout(){
$localStorage.currrntUser
}
function parseJWT(token){
var base64URL, base64;
base64URL = token.split('.')[1];
base64 = base64URL.replace('-', '+').replace('_', '/');
console.log(JSON.parse($window.atob(base64)));
}
function loginStatus(){
if($localStorage.currentUser){
return true;
}else{
return false;
}
}
}]);}());
控制器:
(function(){
angular.module('HomeAutomation')
.controller('loginController', ['$scope', '$location', 'AuthenticationService', function($scope, $location, $localStorage, AuthenticationService){
$scope.isLoggedIn = AuthenticationService.logout();
$scope.logUserIn = function(){
AuthenticationService.login($scope.login.email, $scope.login.password, function(result){
if(result === true){
$location.path('/');
}else{
console.log(result);
}
});
};
$scope.logUserOut = function(){
AuthenticationService.logOut();
}
}]);}());
这是导致错误的行:
$scope.isLoggedIn = AuthenticationService.logout();
显然"AuthenticationService"没有定义。不知道为什么。
你搞砸了依赖序列,你需要从控制器工厂功能中删除$localStorage
,因为$localStorage
在控制器&
.controller('loginController', ['$scope', '$location', 'AuthenticationService',
function($scope, $location, AuthenticationService){
//^^^^^^^^^^removed $localStorage dependency from here
注意:总是要确保当你在DI数组中注入任何依赖项时,它们应该在控制器函数
中以相同的顺序使用
注射不好:
.controller('loginController', ['$scope', '$location', 'AuthenticationService', function($scope, $location, $localStorage, AuthenticationService){
试试这个:
.controller('loginController', ['$scope', '$location', '$localStorage', 'AuthenticationService', function($scope, $location, $localStorage, AuthenticationService){
我不知道你使用哪个构建器,但例如Gulp
,你可以使用gulp-ng-annotate
,它将为你做这项工作,这样你就只能写:
.controller('loginController', function($scope, $location, $localStorage, AuthenticationService){
没有数组的。ng-annotate
会照顾剩下的。
相关文章:
- Ember服务在注入组件并在计算属性中使用后是未定义的
- 未定义服务
- Http服务工厂将未定义的返回到Angular中的控制器
- 角度服务未定义
- 通过C#向区块链服务发送请求时未定义XMLHttpRequest
- 服务变量分配了数据,但在访问时始终未定义
- 承诺返回未定义的服务
- 从服务返回值时$scope控制器的变量是未定义的
- 提供“角服务”;TypeError:无法读取属性'helloConsole'“未定义”;
- 正在从web服务接收未定义的信息
- Angular 1.5.4服务未定义
- 使用路由的未定义服务控制器
- Web服务返回给javascript的值是未定义的
- 返回未定义的角度服务不是一个函数
- 无法使用服务AngularJS - “无法读取未定义的属性'原型'”
- 服务函数返回未定义
- 类型错误:parsed 在 angularjs 服务单元测试中未定义
- 尽管注入了$http,但它在服务中是未定义的
- 注入控制器时,服务未定义
- 输出JSON数组值时服务未定义