附加网址参数的动态AngularJS [英] append URL parameters dynamically AngularJS
问题描述
让我们说我有一个接受的范围方面查询数据的REST风格的端点。这里有一个例子:
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;
});
}
这些方面正在通过在web形式的局部模型传递:
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>
在 getResults
函数的成功回调,我想在上面的例子中的查询参数添加到URL,如下:
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);
});
}
我如何添加URL参数,动态地AngularJS?
How do I append URL parameters dynamically in AngularJS?
推荐答案
$location.path('/newValue').search({key: value});
对于非AngularJS应用:
您可以使用history.js动态地添加URL参数。
然后分裂的网址检索PARAMS和传递给角:
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");
我建议history.js如IE,直到ver9剪掉支持历史记录对象推送状态,如果你要使用IE10 +或谷歌Chrome,使用历史状态对象以更新URL。
希望它可以帮助:)
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 :)
这篇关于附加网址参数的动态AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!