Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - change $location silently - remove query string

Tags:

angularjs

Is there any way to silently change the route in the url bar using angular?

The user clicks a link for the email that goes to:

/verificationExecuted?verificationCode=xxxxxx

When the page loads I want to read the verificationCode and then clear it:

 if($location.path() == '/verificationExecuted'){
     this.registrationCode = this.$location.search()["verificationCode"];
     this.$location.search("verificationCode", null); //Uncomment but make this silent!

     if(registrationCode != null) {
         ....
     }
     else $location.path("/404");      
 }

What happens when I clear it is the remaining part of the route ("/verificationExecuted") remains buts the route re-triggers so it comes around again with no verificationCode and goes straight to 404.

I want to remove the code without doing anything else.

like image 722
parliament Avatar asked Feb 16 '14 18:02

parliament


People also ask

How to get query string value in Angular 8 application?

Many times we need to get query string parameters in angular app. You can easily get query string using ActivatedRoute. i will show you more examples for how to get query string value in angular 8 application. Let's see bellow example, that will help you:

What is the use of $location in angular?

Angular's $location service provides many useful methods for parsing and changing the URL in the browser's address bar. One of these methods, search (), provides a getter and setter for query string parameters.

What are query parameters in angular?

Query parameters in Angular allow for passing optional parameters across any route in the application. Query parameters are different from regular route parameters, which are only available on one route and are not optional (e.g., /product/:id ). In this article, you will reference an example of an application that displays a list of products.

Where are changes in the URL stored in AngularJS?

Any change made in the URL is stored in the $location service in the AngularJS. There are various methods in the $location service such as absUrl (), url ( [URL]), protocol (), host (), port (), path ( [path]), search (search, [paramValue]), hash ( [hash]), replace (), and state ( [state]).


Video Answer


1 Answers

You can always set the reloadOnSearch option on your route to be false.

It will prevent the route from reloading if only the query string changes:

$routeProvider.when("/path/to/my/route",{
   controller: 'MyController',
   templateUrl: '/path/to/template.html',
   //Secret Sauce
   reloadOnSearch: false
});
like image 154
Josh Avatar answered Sep 22 '22 13:09

Josh