16

データをクエリするためにさまざまなファセットを受け入れる 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

私のモジュールは、クエリ オブジェクトを受け入れて検索を実行します。

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 パラメータを動的に追加するにはどうすればよいですか?

4

3 に答える 3

1

上記の例では、$location の path() は次のようになります。

'/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-19T14:45:15.100 に答える
0

実際、$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 を使用)

于 2016-08-19T15:23:53.540 に答える