假设我有一个RESTful端点,它接受一系列方面来查询数据。这里有几个例子:
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
我的模块接受query对象来执行搜索:
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形式通过本地模型传递:
<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中,如上面的示例所示:
$scope.getSearch = function(query){
var search = $search.search(query);
search.getResults(function(results){
$scope.results = results;
search.appendQueryToURL(query);
});
}
如何在AngularJS中动态追加URL参数?
发布于 2016-08-19 22:45:15
对于上面的示例,$location的路径()为
'/search'
它的根是
'http://example.com'
回到他的问题,他没有要求用新的路径更新位置,他要求用新的键值对动态查询更新位置!
因此,为了做到这一点,它只是一个日常的jQuery $x(newX)!
$location.search({type: x, location: y, radius: z});
或
$location.search({type: x, location: y});
这都是jQuery !!
如果他想将路径从搜索更改为其他路径,他可以这样做:
$location.path('/otherPath');
//his new location would be 'example.com/otherPath'
发布于 2016-08-19 23:23:54
实际上,与使用$location调用数据服务端点不同,通常的方法是
<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>
在getDataService()中做一个
$http.get( serviceUrl, config ).success( ...
你的serviceUrl是
example.com/search/Facility/Wayne,NJ/3/f
解析之后(使用sprintf)
https://stackoverflow.com/questions/17491054
复制相似问题