javascript - 如何使用 AngularJS 处理数组?

标签 javascript angularjs

我有一个 API 返回以下简单的 JSON 数组:

[
  "efd98ad-first_key",
  "100eb0a-second_key"
]

我正在尝试使用 Angular 将其呈现为表格:

<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>{{bucket}}</td>
      </tr>
    </table>
  </div>
</div>

JS:

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

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

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

});

除了抛出“未捕获的对象”错误之外,它什么也不做。

我如何调试它失败的原因?

最佳答案

您将值存储在 $scope.response 中,但您在 ng-repeat 处使用 $scope.bucket:

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

};

关于javascript - 如何使用 AngularJS 处理数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24071051/

相关文章:

javascript - 为什么可以在 AngularJS 的路由上定义 Controller ?

angularjs - 所见即所得的 Angular2 和双向绑定(bind)

css - AngularJS - 从多个选项更改 1 <div> 上的多个 CSS 类

javascript - 将 [2,x,y] 矩阵转换为 [y+1,2x] 以便在 dygraphs 中使用

javascript - 那里有什么好的 JavaScript 散列(代码/表)实现吗?

javascript - react : is there a better pattern for higher-order component rendering?

javascript - Uncaught Error : [$injector:modulerr] - Heatmap using d3 and AngularJS

javascript - 删除按钮不起作用

javascript - 获取表单更改时的元素 id

javascript - ng-change 和 ng-click 在切换按钮 bootstrap-toggle 中不起作用