简体   繁体   English

如何在Angular的GET请求中使用URL参数?

[英]How to use URL parameters in a GET request with Angular?

I have very simple application that has two features. 我有两个功能,非常简单的应用程序。 One, it lists all of the keys two, it is supposed to display one particular key (and the associated value). 第一,列出所有键,第二,应该显示一个特定键(以及关联的值)。

Here is the first part that list the keys: 这是列出密钥的第一部分:

var app = angular.module('flipDaSwitch', ['ngRoute']);

app.controller('ListKeys', function($scope, $http) {

  $scope.getKeys = function() {
    $http.get('/api/keys').
      success(function (data) {
        $scope.buckets = data;
      }).
      error(function (data){
        $scope.buckets = {}
      });
  };

});

I am wondering how could I get a particular key from the API. 我想知道如何从API获取特定密钥。 The current HTML: 当前的HTML:

<div name="listkeys" class="container">
  <div class="starter-template">
    <div ng-bind-html="message"></div>
    <table class="table table-striped table-bordered table-condensed sortable" ng-init="getKeys()">
      <tr>
        <th>Bucket:</th>
      </tr>
      <tr ng-repeat="bucket in buckets">
        <td>
          <a ng-href="/show_key.html#/key/{{bucket}}">{{bucket}}</a>
        </td>
      </tr>
    </table>
  </div>
</div>

Obviously the show_key.html gets the key in the URL like this: 显然,show_key.html可以通过以下方式获取URL中的密钥:

/show_key.html#/key/efd1ae55a5-random_string

I am not sure how could I issue a get request based on the URL parameters. 我不确定如何根据URL参数发出get请求。

Inject $routeParams in controller: 在控制器中注入$routeParams

app.controller('ListKeys', function($scope, $http, $routeParams) {
...

Docs 文件

Also there is a bunch of questions about this. 关于此还有很多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM