AngularJS 使用 REST 服务

use*_*073 5 angularjs


我需要编写一个 AngularJS 页面,该页面显示来自 REST 服务的一些 JSON 数据。REST 服务在调用时会显示以下 JSON 示例数据:

[{"key":"ABX1234","value":"Network Hub"}]
Run Code Online (Sandbox Code Playgroud)

这是我用来检索数据的 HTML 页面:

<html ng-app>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
        <script>
        function Hello($scope, $http) {
            $http.get('http://host/json').
                success(function(data) {
                    $scope.json = data;
                });
        }
        </script>
    </head>
    <body>
        <div ng-controller="Hello">
            <p>The ID is {{json.key}}</p>
            <p>The content is {{json.value}}</p>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

不幸的是,没有任何内容显示为 json.key 或 json.value。你能帮我找出问题所在吗?
谢谢!

Dr *_*ack 3

应该是这样的:

 <div ng-controller="Hello">   
    <div ng-repeat="json in json">
                <p>The ID is {{json.key}}</p>
                <p>The content is {{json.value}}</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

您正在获取对象数组。它需要迭代。