當我不希望查詢字符串持續存在時,我在AngularJS中通過路由更改持久查詢字符串時遇到問題。我創建了一個plunker(下面)來嘗試重新生成此問題,但我發現查詢字符串通常不會跨路由更改持續存在,如this question中所述。如何避免使用AngularJS保持查詢字符串ngRoute
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');
};
});
我剛剛升級到1.3 AngularJS卻發現在1.2以前的版本相同的行爲。(12 | 24)。不幸的是,帶有問題的源代碼是專有的,所以我不能分享導致問題的確切代碼。
我已經嘗試了一切,但調試到角來源找出爲什麼我的查詢字符串持續跨路線變化。有沒有其他人遇到過這個問題或知道如何關閉它?
更新
請注意,如簡單地stripping off the query string解決方案將無法正常工作,我的許多環節實際上包括了一些查詢字符串參數的默認值。