2

I type url and press enter http://myservertest.com?auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

This one will call a service with auth param to authenticate, after authorization is successful, I want my url should be like this http://myservertest.com/mainpage without refresh app controller again.

I tried windows.location = "/" then it will refresh the page.

How can I do without refresh APP Controller ?

user4437473
  • 67
  • 1
  • 5

1 Answers1

0

Just in case someone stumbles about this again after I stumbled about this after 7 years:

Usually you solve it by using $location:

$location.url('<url>');
$location.replace();

But... default html5Mode this will not replace window.location.search query parameters like

http://localhost/path?auth=xxx&andso=on

Without html5Mode (enabled:false by default) you need to care about pushState yourself before the AngularJs application runs:

Solution is to use...

window.history.replaceState(null, window.name, window.location.origin + window.location.path + window.location.hash)

(an example replacing the currently shown location url without parameters)

.. in an angular.provider() or angular.config():

In a provider when offering a reusable service:

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 = ...


}]);

and/or in an config when using providers:

angular.module('app').config([
  'AuthServiceProvider',
  'someConfig',
  function (AuthServiceProvider, someConfig) {

    window.history.replaceState(.....)
    // or in
    // AuthServiceProvider.readQuery(...)
    
}]);
reifi
  • 311
  • 2
  • 10