简体   繁体   中英

AngularJS, how to change query string

For example, i have this url: 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 .

How i can do it? I tried $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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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