无法在run方法angularjs中注入服务
发布时间:2020-12-17 17:31:55 所属栏目:安全 来源:网络整理
导读:我刚刚开始使用AngularJS,因为我总是习惯在服务器端工作,我在完成任务时遇到了一些困难,特别是调试代码并找出错误可能是什么. 我见过很多人使用事件$stateChangeStart来验证用户身份验证.我正在尝试这样做,但是当我尝试将我的服务注入run方法时,我总是得到未
我刚刚开始使用AngularJS,因为我总是习惯在服务器端工作,我在完成任务时遇到了一些困难,特别是调试代码并找出错误可能是什么.
我见过很多人使用事件$stateChangeStart来验证用户身份验证.我正在尝试这样做,但是当我尝试将我的服务注入run方法时,我总是得到未定义的服务.这是我的电话: angular .module('module_name') .config(config) .run(function($rootScope,$state,authService) { $rootScope.$state = $state; $rootScope.$on("$stateChangeStart",function(event,toState,toParams,fromState,fromParams) { if (toState.authenticate && !authService.isLoggedIn()) { $state.go("login"); event.preventDefault(); } }); }); 码: 编辑1: 包括: <!-- Angular App Script --> <script src="js/app.js"></script> <script src="js/services/authService.js"></script> <script src="js/config.js"></script> <script src="js/directives.js"></script> <script src="js/controllers/MainCtrl.js"></script> <script src="js/controllers/LoginCtrl.js"></script> JS / app.js: (function () { angular.module('module_name',[ 'ui.router','ui.bootstrap','LocalStorageModule',]) })(); authService.js: angular .module('module_name') .factory('authService',['$http','$q','$rootScope','localStorageService',function ($http,$q,$rootScope,localStorageService) { var serviceBase = 'http://url'; var authServiceFactory = {}; var _authentication = { isAuth: false,userName : "" }; var _saveRegistration = function (registration) { _logOut(); return $http.post(serviceBase + 'account/register',registration).then(function (response) { return response; }); }; var _login = function (loginData) { var deferred = $q.defer(); $http.get(serviceBase + 'account/token',{ headers: { 'username': loginData.userName,'password': loginData.password } }).success(function (response,status,headers,config) { localStorageService.set('authorizationData',{ token: headers('token'),userName: loginData.userName }); _authentication.isAuth = true; _authentication.userName = loginData.userName; deferred.resolve(response); }).error(function (err,status) { _logOut(); deferred.reject(err); }); return deferred.promise; }; var _logOut = function () { localStorageService.remove('authorizationData'); _authentication.isAuth = false; _authentication.userName = ""; }; var _isLoggedIn = function() { return _authentication.isAuth; } authServiceFactory.isLoggedIn = _isLoggedIn; return authServiceFactory; }]); config.js: function config($stateProvider,$urlRouterProvider,$locationProvider) { $urlRouterProvider.otherwise("/"); $stateProvider .state('login',{ url: "/",templateUrl: "login.html",controller: function($scope) { $('body').addClass('gray-bg'); },data: { pageTitle: 'Example view' },authenticate: false }) .state('main',{ url: "/main",templateUrl: "views/main.html",authenticate: true }) .state('minor',{ url: "/minor",templateUrl: "views/minor.html",authenticate: true }); //$locationProvider.html5Mode(true); } angular .module('module_name') .config(config) .run(['$rootScope','$state','authService',function ($rootScope,fromParams) { if (toState.authenticate && !authService.isLoggedIn()) { $state.go("login"); event.preventDefault(); } }); }]); 我已经阅读了一点,看到有订单注入服务,提供商和工厂,但我无法让它工作. 解决方法
我不确切地知道错误的原因,但上面的代码是有效的.
对于那些可能面临同样问题的人,我认为错误是以下行: .run(['$rootScope',authService) { 在此之前,它没有括号[],只有这样注入authService: .run('authService',function (authService) { 所以,记得添加all并添加括号.我真的不知道为什么这个括号,如果有或没有它们有任何区别(有人可以帮我这个),但这是我的代码工作的方式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |