2014-10-28 24 views
1

当我不希望查询字符串持续存在时,我在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解决方案将无法正常工作,我的许多环节实际上包括了一些查询字符串参数的默认值。

回答

0

我发现问题的前提是错误的。我遇到过这种情况,我手动尝试使用$location.path路由到新的URL。我通过允许锚点单击通过并作为链接进行处理来解决此问题。