首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS用HTML5Mode = false删除HTML5Mode参数

AngularJS用HTML5Mode = false删除HTML5Mode参数
EN

Stack Overflow用户
提问于 2015-01-09 15:54:27
回答 1查看 235关注 0票数 2

我输入url并按enter http://myservertest.com?auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

这个程序将调用一个带有auth param的服务进行身份验证,在授权成功之后,我希望我的url应该像这个http://myservertest.com/mainpage一样,而不需要刷新应用程序控制器。

我尝试了windows.location = "/“,然后它会刷新页面。

如果没有刷新应用程序控制器,我怎么做?

EN

回答 1

Stack Overflow用户

发布于 2022-03-24 13:56:09

万一我七年后又被绊倒了:

通常通过使用$location来解决这个问题:

代码语言:javascript
复制
$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():中

在提供可重用服务的提供者中:

代码语言:javascript
复制
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 = ...


}]);

和/或在配置中使用提供程序时:

代码语言:javascript
复制
angular.module('app').config([
  'AuthServiceProvider',
  'someConfig',
  function (AuthServiceProvider, someConfig) {

    window.history.replaceState(.....)
    // or in
    // AuthServiceProvider.readQuery(...)
    
}]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27864357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档