I have a problem with persisting query strings across route changes in AngularJS when I don't want the query string to persist. I created a plunker (below) to try to re-produce this issue, but I see that the query string doesn't generally persist across route changes, as noted in this question.
http://plnkr.co/edit/SD8QMdSeAfAnVP3ZNHK4?p=preview
var demo = angular.module('demo', ['ngRoute']);
demo.config(function ($routeProvider) {
$routeProvider.when('/page1', {
template:
"<h1>{{ctrl.title}}</h1>" +
"<ul>" +
"<li>Page 1</li>" +
"<li><a href=\"#/page2/Title\">Page 2</a></li>" +
"</ul>" +
"<p>{{ctrl.currentUrl}}</p>" +
"<p>{{ctrl.queryString}}</p>",
controller: 'Page1',
controllerAs: 'ctrl'
});
$routeProvider.when('/page2/:param', {
template:
"<h1>{{ctrl.title}}</h1>" +
"<ul>" +
"<li><a href=\"#/page1\">Page 1</a></li>" +
"<li>Page 2</li>" +
"</ul>" +
"<p>{{ctrl.currentUrl}}</p>" +
"<p>{{ctrl.queryString}}</p>" +
"<button ng-click=\"ctrl.appendQS()\">Append query string</button>",
controller: 'Page2',
controllerAs: 'ctrl'
});
$routeProvider.otherwise({ redirectTo: '/page1' });
});
demo.controller('Page1', function Page1($location) {
this.title = 'Page 1';
this.currentUrl = $location.url();
this.queryString = $location.search();
});
demo.controller('Page2', function Page2($routeParams, $location) {
this.title = $routeParams.param;
this.currentUrl = $location.url();
this.queryString = $location.search();
this.appendQS = function () {
$location.search('foo', 'bar');
};
});
I just upgraded to AngularJS 1.3 but found the same behavior in previous versions of 1.2.(12|24). Unfortunately the source code with the problem is proprietary, so I can't share the exact code causing the problem.
I have tried everything but debug into the Angular source to find out why my query string persists across route changes. Has anyone else encountered this issue or know how to turn it off?
Update
Please note that solutions such as simply stripping off the query string will not work as many of my links actually include default values for some query string parameters.