Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
74
rated 0 times [  78] [ 4]  / answers: 1 / hits: 40166  / 11 Years ago, fri, july 5, 2013, 12:00:00

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?


More From » angularjs

 Answers
2

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


[#77176] Thursday, July 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;