16

Let's say I have a RESTful endpoint that accepts a range of facets to query data. Here's a few examples:

example.com/search?type=Doctor&location=Boston,MA&radius=2  
example.com/search?type=Facility&location=Wayne,NJ&radius=3&gender=f  
example.com/search?type=Doctor&location=Patterson,NJ

My module accepts the query object to perform the search:

console.log(query);
{
  type:'Doctor',
  location:'Boston,MA',
  radius:'2'
}

$scope.getSearch = function(query){
  var search = $search.search(query);
  search.getResults(function(results){
    $scope.results = results;
  });
}

These facets are being passed through a local model in a web form:

 <input type="text" ng-model="query.location"/>
 <input type="text" ng-model="query.radius"/>
 <button type="button" ng-click="getSearch(query)">Search</button>

In the success callback of the getResults function, I'm trying to append the query parameters to the URL like in the examples above:

$scope.getSearch = function(query){
  var search = $search.search(query);
  search.getResults(function(results){
    $scope.results = results;
    search.appendQueryToURL(query);
  });
}

How do I append URL parameters dynamically in AngularJS?

2
  • 3
    are you looking for $location.search()? Commented Jul 5, 2013 at 14:52
  • @KhanhTO Holy... yes. It even accepts key value pairs. Commented Jul 5, 2013 at 14:56

3 Answers 3

39

Using $location

$location.path('/newValue').search({key: value});

For Non AngularJS applications:

You can use history.js to append parameters dynamically to the url. then split the url to retrieve the params and pass to angular:

History.replaceState({state:1}, "State 1", "?Param1=something");

I am suggesting history.js as IE until ver9 didnt support history object push state, if you are going to use IE10+ or google chrome, use the history state object to update the url. Hope it helps :)

Sign up to request clarification or add additional context in comments.

5 Comments

There's really no native AngularJS method that does this for you already?
docs.angularjs.org/guide/dev_guide.services.$location reflecting this in my answer . Please upvote if this is what you are looking for
Havent used it alot but please follow this, this might give you a better idea: stackoverflow.com/questions/14174394/…
A nice interface for touching the url; I was a bit disappointed that it requires a hash (possibly only if the url ends with a /), but what can ya do.
Chaining the path and search this way will cause the existing parameters to be replaced. The op's question is to append. Hence, append the parameter using $location.search(key, paramValue) and then call $location.path(yourPath).
1

For the example above, the path() of your $location would be

'/search' 

and the root is

'http://example.com' 

Going back to his question, he did not ask to update the location with new path, he asked to update the location with new key-value pairs query dynamically!

So to do that, it's just everyday jQuery $x(newX)!

$location.search({type: x, location: y, radius: z}); 

or

$location.search({type: x, location: y}); 

It's all jQuery !!

If he wants to change the path from search to something else, he can do:

$location.path('/otherPath'); 
//his new location would be 'example.com/otherPath' 

Comments

0

In fact, instead of using $location to call your data-service endpoint, a normal way to do that is

<form id='searchForm' ng-submit="getDataService()" ng-controller="aController">
<input ng-model="type"/>
<input ng-model="location"/>
<input ng-model="radius"/>
<input ng-model="gender"/>
<button type='submit'/>
</form>

and in getDataService() do a

$http.get( serviceUrl, config ).success( ...

with your serviceUrl being

example.com/search/Facility/Wayne,NJ/3/f

after parsing (using sprintf)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.