AngularJS,如何更改查询字符串

AngularJS, how to change query string

本文关键字:查询 字符串 何更改 AngularJS      更新时间:2023-09-26

例如,我有一个url:http://local.com/。当我在SearchController中调用函数时,我想设置text=searchtext并获得如下url:http://local.com/?text=searchtext

我该怎么做?我尝试了$location.search('text', 'value');,但得到了这个网址:

http://local.com/#?text=searchtext

$scope.searchTracks = function() {
        Search.search.get($scope.params, function(data) {
            /** Set params in query string */
            $location.search('text', $scope.text);
            $location.search('sorted', $scope.sorted);
        });
    }

您还需要指定路径:

$location
  .path('/path/to/new/url')
  .search({
    'text': $scope.text,
    'sorted': $scope.sorted
  });

最终的url将类似于:

http://localhost/path/to/new/url?text={{$scope.text}}&sorted={{$scope.sorted}}

另一种方法是手动设置:

$location.url('/path/to/new/url?text' + $scope.text + '&sorted=' + $scope.sorted);