首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Angular $location通过动态更改键值参数来设置新的url位置?

如何使用Angular $location通过动态更改键值参数来设置新的url位置?
EN

Stack Overflow用户
提问于 2013-07-05 22:19:41
回答 2查看 39.8K关注 0票数 16

假设我有一个RESTful端点,它接受一系列方面来查询数据。这里有几个例子:

代码语言:javascript
复制
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对象来执行搜索:

代码语言:javascript
复制
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形式通过本地模型传递:

代码语言:javascript
复制
 <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中,如上面的示例所示:

代码语言:javascript
复制
$scope.getSearch = function(query){
  var search = $search.search(query);
  search.getResults(function(results){
    $scope.results = results;
    search.appendQueryToURL(query);
  });
}

如何在AngularJS中动态追加URL参数?

EN

回答 2

Stack Overflow用户

发布于 2016-08-19 22:45:15

对于上面的示例,$location的路径()为

代码语言:javascript
复制
'/search' 

它的根是

代码语言:javascript
复制
'http://example.com' 

回到他的问题,他没有要求用新的路径更新位置,他要求用新的键值对动态查询更新位置!

因此,为了做到这一点,它只是一个日常的jQuery $x(newX)!

代码语言:javascript
复制
$location.search({type: x, location: y, radius: z}); 

代码语言:javascript
复制
$location.search({type: x, location: y}); 

这都是jQuery !!

如果他想将路径从搜索更改为其他路径,他可以这样做:

代码语言:javascript
复制
$location.path('/otherPath'); 
//his new location would be 'example.com/otherPath' 
票数 1
EN

Stack Overflow用户

发布于 2016-08-19 23:23:54

实际上,与使用$location调用数据服务端点不同,通常的方法是

代码语言:javascript
复制
<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()中做一个

代码语言:javascript
复制
$http.get( serviceUrl, config ).success( ...

你的serviceUrl是

代码语言:javascript
复制
example.com/search/Facility/Wayne,NJ/3/f

解析之后(使用sprintf)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17491054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档