我有一个 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/