简体   繁体   English

AngularJS,如何更改查询字符串

[英]AngularJS, how to change query string

For example, i have this url: http://local.com/ . 例如,我有这个网址: http://local.com/ : http://local.com/ When i called function in my SearchController i want set text=searchtext and get url like this: http://local.com/?text=searchtext . 当我在SearchController调用function时,我想设置text=searchtext并获取如下网址: http://local.com/?text=searchtext : text=searchtext

How i can do it? 我该怎么办? I tried $location.search('text', 'value'); 我尝试了$location.search('text', 'value'); but got this url: 但是得到了这个网址:

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

        });

    }

You also need to specify the path : 您还需要指定路径:

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

And the final url will be something like: 最终的网址将类似于:

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

Another way is to set them manually: 另一种方法是手动设置它们:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM