AngularJS从webapi获取数据并存储在表中

时间:2017-08-16 15:19:18

标签: html angularjs json rest

我创建了一个rest web api并使用AngularJS,我想接收这些json数据并将它们存储在一个表中。我是AngularJS的新手。我能够获得所有json数据,但我想将它们分成每一行。我不确定我是否做得不对,但这是我的代码:

的index.html

<!DOCTYPE html>
<html ng-app="demoApp">
<head>
<meta charset="UTF-8">
<title>Angular</title>
<script src="lib/angular.js"></script>
<script src="angularDemo.js"></script>
</head>
<body>
    <div ng-controller="demoController">
        <table>
            <tr>
                <th>Id</th>
                <th>Question</th>
            </tr>
            <tr ng-repeat=" quiz values">
                <td>{{result.id}}</td> <!-- Does not get any value-->
                <td>{{result.question}}</td> <!-- Does not get any value-->
            </tr>
        </table>
        <h1>{{result}}</h1> <!-- gets all the json data -->
    </div>
</body>
</html>

JS

var app = angular.module("demoApp", []);

app.controller("demoController", function($scope, $http) {
    $http.get("http://localhost:8080/quiz/webapi/quiz")
    .then(function(response) {
        $scope.result = response.data;
    });
});

1 个答案:

答案 0 :(得分:1)

您的ng-repeat不好,

如果我理解您的结果数组位于$scope.result,那么您必须执行此类ng-repeat

<tr ng-repeat="row in result">
   <td>{{row.id}}</td> 
   <td>{{row.question}}</td> 
</tr>