【问题标题】:Nested JSON from AWS Lambda in AngularAngular 中来自 AWS Lambda 的嵌套 JSON
【发布时间】:2020-02-21 01:16:14
【问题描述】:

我正在使用以下内容将 RESTful API 信息消耗到 AngularJS 中:

angular.module('demo', [])
.controller('Hello', function($scope, $http) {
    $http.get('restapi address').
        then(function(response) {
            $scope.content = response.data;
            console.log(response)
        });
});

我在我的索引中使用响应数据如下(这部分不起作用,见下文):

<div ng-controller="Hello">
        <p>The ID is {{content.course-content}}</p>
        <p>The content is {{content.course-id}}</p>
        <p>The content is {{content.course-title}}</p>
</div>

这是来自 API 调用的 JSON 响应:

{
    "body": [{
        "course-content": "In order for you to be successful in the program",
        "course-id": "supervised-ml",
        "course-title": "Supervised ML"
    }]
}

如何显示课程内容、课程 ID 和课程标题?我知道这些是嵌套的,但不确定如何引用控制器中的每个项目进行渲染。

【问题讨论】:

    标签: json angularjs


    【解决方案1】:

    只需对 HTML 中的插值进行小修正即可:

    <div ng-controller="Hello">
        <p>The ID is {{content['course-content']}}</p>
        <p>The content is {{content['course-id']}}</p>
        <p>The content is {{content['course-title']}}</p>
    </div>
    

    属性名称中的连字符需要包含在字符串文字中,以确保它们正确引用。

    【讨论】:

    • 感谢您的建议,但修改无效(同样的问题)。
    • 啊,我错过了一些东西——JSON 响应 sn-p 你包含了你分配给 $scope.contentresponse.data 的值吗?如果是,那么您必须打开第一个项目的包装(例如$scope.content = response.data.body[0])。
    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多