Javascript AngularJS$rootScope.authenticated重置?
似乎当我从一个页面重定向到我的主页时,我的根范围重置了 以下是登录后的重定向代码:Javascript AngularJS$rootScope.authenticated重置?,javascript,angularjs,Javascript,Angularjs,似乎当我从一个页面重定向到我的主页时,我的根范围重置了 以下是登录后的重定向代码: self.authUser = function() { LoginService.authUser($scope.inputEmail, $scope.inputPassword, 'inf') .then(function(d) { $rootScope.authentica
self.authUser = function() {
LoginService.authUser($scope.inputEmail, $scope.inputPassword, 'inf')
.then(function(d) {
$rootScope.authenticated = true;
$window.location.href = '/job';
}, function(errResponse) {
$scope.errorLogin = true;
$rootScope.authenticated = false;
});
};
以下是主页的代码:
<script src="<c:url value="/resources/js/application.js"/>"></script>
<script src="<c:url value="/resources/js/service/main_service.js"/>"></script>
<script
src="<c:url value="/resources/js/controller/main_controller.js"/>"></script>
<title>Insert title here</title>
</head>
<body ng-app='application'>
<div class='container' ng-controller='MainController as ctrl'>
<span ng-show='authenticated'>lllllllllllllllloloooooo</span>
<button class="btn btn-lg btn-primary btn-block" ng-click='ctrl.authorize()'>Sign
in</button>
</div>
</body>
尝试
$location.path(“/”);
$scope.$apply()
及
$window.location.assign('/job')
也
$location不更改页面&$window刷新我的rootscope我实际上没有正确使用rootscope,nGroute也没有正确使用。您使用的是html5还是基于哈希的路由?您使用的是哪种路由系统?角路线?用户界面路由器?您当前正在重新加载应用程序,这将重置状态。您需要正确使用路由以防止重新加载。@datasage我使用的是ngRoute
'use strict';
var App = angular.module('application', [ 'ngRoute', 'ngCookies' ]).config(
[ '$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl : '/job/',
controller : 'main_controller'
}).when('/login', {
templateUrl : '/job/login',
controller : 'login_controller'
}).otherwise({
redirectTo : '/'
});
} ]);
$timeout(function () {
$location.path("/");
});