2014-06-11 45 views
1

我有非常简单的应用程序,有两个功能。一,它列出了所有的键二,它应该显示一个特定的键(和相关的值)。如何在使用Angular的GET请求中使用URL参数?

这里是列表中的键第一部分:我想知道我怎么能得到从API特定的键

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 = {} 
     }); 
    }; 

}); 

。目前的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> 

显然show_key.html得到像这样的URL的关键在于:

/show_key.html#/key/efd1ae55a5-random_string 

我不知道我怎么会发出基于URL参数的GET请求。

+0

[AngularJS可能使用ngRoute获取当前URL参数](http://stackoverflow.com/questions/20655877/angularjs-get-current-url-parameters-using-ngroute) – zishe

回答

1

进样$routeParams在控制器:

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

Docs

也有一堆关于这个问题。

相关问题