Change Service Portal URL without reloading the page.

amanda_l_webb
Kilo Contributor

Has anyone figured out how to change the URL without reloading the page?

I would like to alter the URL so that my users can send it out and it will pre-populate widget information. Problem is that it reloads. I have tried to figure out how to use ngRoute, ui.route, and history.pushState. All seem to reload the page. Any other thoughts?

Thanks!

1 ACCEPTED SOLUTION

You are right, it seems like that the reason for this is that the $locationChangeStart events gets triggered more than once so you have to build a condition within the event listener.
What has worked for me is the following:



The event listener:


var originalUrl, initUrl;


$rootScope.$on("$locationChangeStart", function(e, newUrl){


        if(newUrl != originalUrl && newUrl != initUrl){


                  $window.history.replaceState(newUrl, $document.title, newUrl);


        }


        originalUrl = newUrl;


        e.preventDefault();


});



The URL change:


$scope.changeURL = function(){


        initUrl = $location.absUrl();



        //Set your desired URL parameters here, you could also extend the current ones with:        


        /*


        var params = $location.search()


        angular.extend(params, {


                  someParam:'test'


        });


          */      


        var params = {


                  someParam:'test',


                  anotherParam:'anotherTest'


        };



        //triggers the $locationChangeStart event


        $location.search($httpParamSerializer(params));


}



Dont forget to inject $document, $httpParamSerializer, $location, $scope and $rootScope in your controller.


Let me know if this worked out for you.


View solution in original post

30 REPLIES 30

Rama Chandra D
Kilo Guru

Hi Amanda,



Can be more specific? Do you need to share the URL of a record to other users? I couldn't quite get what you mean by pre-populate widget information?



Darshak


Basically I want to change the URL without reloading the page. I haven't found a way of doing that yet within Service Portal.


Hi Amanda,



Do you want set the URL dynamically for specific link . Can you provide more details.



Regards,


Harish Murikinati.


Basically, I have a page where you select something to search, search for an item, then can view details of that item. Along that process, I want to put the selections into the URL so that a user can send the URL to another colleague to view, and that person won't have to do the search themselves.



Think of it like filtering on SN tables. It throws your search info into the URL so you can use the URL to get back where you are. That's what I am looking to do, but I do not want the page to refresh every time I alter the URL.