我输入url并按enter http://myservertest.com?auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxx
这个程序将调用一个带有auth param的服务进行身份验证,在授权成功之后,我希望我的url应该像这个http://myservertest.com/mainpage一样,而不需要刷新应用程序控制器。
我尝试了windows.location = "/“,然后它会刷新页面。
如果没有刷新应用程序控制器,我怎么做?
发布于 2022-03-24 13:56:09
万一我七年后又被绊倒了:
通常通过使用$location来解决这个问题:
$location.url('<url>');
$location.replace();但是.默认的html5Mode --这不会取代window.location.search查询参数,例如
http://localhost/path?auth=xxx&andso=on
如果没有html5Mode (默认情况下启用:false),那么在AngularJs应用程序运行之前,需要自己关心pushState:
解决方案是使用.
window.history.replaceState(null, window.name, window.location.origin + window.location.path + window.location.hash)
(替换当前显示的不带参数的位置url的示例)
..在angular.provider()或angular.config():中
在提供可重用服务的提供者中:
angular.module('app').provider('AuthService', [
'someConfig',
function(someConfig){
if (someConfigObject.doItBeforeStartup) {
// ... check for parameter in window.location.search or whatever
window.history.replaceState(null, window.name, window.origin + window.path + window.hash)
}
this.readQuery = function(cfg){./* you can do it also here and call it in config*/..}
this.$get = ...
}]);和/或在配置中使用提供程序时:
angular.module('app').config([
'AuthServiceProvider',
'someConfig',
function (AuthServiceProvider, someConfig) {
window.history.replaceState(.....)
// or in
// AuthServiceProvider.readQuery(...)
}]);https://stackoverflow.com/questions/27864357
复制相似问题