1

当我不希望查询字符串保留时,我在 AngularJS 中的路由更改中保留查询字符串时遇到问题。我创建了一个 plunker(如下)来尝试重现此问题,但我发现查询字符串通常不会在路由更改中持续存在,如本问题所述。

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');
  };
});

我刚刚升级到 AngularJS 1.3,但在以前的 1.2.(12|24) 版本中发现了相同的行为。不幸的是,有问题的源代码是专有的,所以我不能分享导致问题的确切代码。

我已经尝试了所有方法,但调试到 Angular 源代码以找出为什么我的查询字符串在路由更改中仍然存在。有没有其他人遇到过这个问题或知道如何关闭它?

更新

请注意,诸如简单地删除查询字符串之类的解决方案将不起作用,因为我的许多链接实际上包含某些查询字符串参数的默认值。

4

1 回答 1

0

我发现问题的前提是错误的。我有一个发生这种情况的场景,我正在手动尝试使用$location.path. 我通过允许锚点单击通过并作为链接处理来解决此问题。

于 2014-10-28T20:29:23.297 回答