当用户输入正确的用户名和密码时,我想重定向到另一个位置。但是当我在这里使用$location.path('dashboard')
时,浏览器的URL被更改但该页面未加载。当使用ctrl + R刷新页面或点击浏览器的刷新图标时,则会加载适当的页面。
$http.post('/login', $scope.userInfo)
.success(function (data) {
//Check if the authentication was a success or a failure
//Successful POST here does not indicate success of authentication
if (data.status === "authentication success") {
//Proceed to load the dashboard for the user
$location.path('/dashboard');
} else if (data.status === "authentication error") {
//Inform user of the error
$scope.errorMessage = data.error;
$scope.showErrorMessage = true;
}
})
.error(function (error) {
$scope.errorMessage = "Error while attempting to authenticate. Check log.";
$scope.showErrorMessage = true;
});
};
}]);
答案 0 :(得分:24)
您是否尝试过使用$ scope。$ apply之后?例如:
if(!$scope.$$phase) $scope.$apply()
这种简短的方法也常常派上用场:
答案 1 :(得分:13)
引用https://docs.angularjs.org/guide/$location
“$ location服务允许您仅更改URL;它不允许您重新加载页面。当您需要更改URL并重新加载页面或导航到其他页面时,请使用较低级别的API ,$ window.location.href。“
因此,例如,而不是使用:$location.path("/path/to/something");
使用此:$window.location.href = "/path/to/something";
$window.location.href
会更改网址 AND 加载新网页。
希望有所帮助。
答案 2 :(得分:5)
如果你没有 $ scope (就像服务一样) 然后你可以使用
$location.path('/path');
if (!$rootScope.$$phase) $rootScope.$apply();
答案 3 :(得分:3)
试试这个(别忘了注入$ timeout):
$timeout(function () {
$location.path('/dashboard');
}, 0);
答案 4 :(得分:0)
使用$ timeout(function){}这将起作用
答案 5 :(得分:-2)
使用$rootScope.$evalAsync
例如:
$rootScope.$evalAsync(function() {
$location.path('/dashboard');
});
答案 6 :(得分:-6)
尝试:
$location.path('/dashboard');
$location.reload();