我的应用程序中有两个 Angular 应用程序。第一个应用程序基本上只是登录页面,第二个应用程序是其他所有内容。这两个应用程序共享几个服务,包括一个控制登录信息/逻辑的服务。
当用户使用“记住我”功能时会出现问题。我们希望他们在浏览到登录页面时自动登录并重定向到登录状态页面。自动登录位工作正常,但是当我们尝试将用户重定向到第二个应用程序中的状态页面时,他们陷入无限循环,登录页面一遍又一遍地刷新。 (有趣的是,如果用户未登录,请输入相应的登录信息并单击
似乎重定向有效,因为 url 确实更新正确并且页面刷新,但是内容是错误的(仍然是旧的 Controller 和 View ),所以它不断地重新做同样的事情——检查是否登录,如果是则重定向。 我如何确保我确实转移到第二个应用程序并在重定向时加载正确的 View / Controller ?
代码:
twPublicApp.controller('PublicLoginCtrl', function ($scope, $http, LoginData, BrowserStorageService, $window, $location) {
window.changeApiRoot('/api/');
$scope.username = '';
$scope.password = '';
$scope.remember = false;
$scope.errorMessage = '';
$scope.authenticating = false;
if (LoginData.get('SessionToken', null, true) !== null) {
// Check if still has a valid session
$scope.authenticating = true;
LoginData.checkSession(LoginData.get('SessionToken'), LoginData.get('Location'))
.success(function(data) {
if (data.Result === true) {
console.log('User looks to be already logged in... sending on');
BrowserStorageService.changeStore('localStorage');
LoginData.set(data);
//This is the line giving me problems. I've tried these two versions, and a few others, and nothing seems to work properly.
//window.location.href = '/console#/status';
$window.location = '/console#/status';
$scope.authenticating = false;
} else {
$scope.authenticating = false;
LoginData.set({});
}
})
.error(function(jqXHR, textStatus, errorThrown) {
$scope.authenticating = false;
LoginData.set({});
});
}
$scope.authenticate = function() {
$scope.authenticating = true;
LoginData.authenticate($scope.username, $scope.password, $scope.remember)
.success(function(data) {
if (data.Result === true) {
if ($scope.remember) {
BrowserStorageService.changeStore('localStorage');
} else {
BrowserStorageService.changeStore('sessionStorage');
}
LoginData.set(data);
//Interestingly, this line works perfectly.
window.location.href = '/console#/status';
return;
}
$scope.authenticating = false;
})
.error(function(jqXHR, textStatus, errorThrown) {
$scope.authenticating = false;
});
};
});
最佳答案
Using $window or $location to Redirect in AngularJS
我相信上述链接将帮助您解决问题。
关于javascript - 从一个 AngularJS 应用程序切换到另一个——页面内容未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46708350/