大佬教程收集整理的这篇文章主要介绍了无法在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方法angularjs中注入服务全部内容,希望文章能够帮你解决无法在run方法angularjs中注入服务所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。