来自Ajax GET的HTML中的角度显示响应

时间:2019-06-30 21:24:43

标签: javascript html angularjs ajax

提前道歉。我已经发表了十几篇相关文章,但都没有成功。

这是我的控制器。

app.controller('testController', function ($scope) {
    var myApi = 'https://jsonplaceholder.typicode.com/posts';
    $.ajax({
        url: myApi,
        type: 'GET',
        success: function (response) {
            console.log(response[0]);
            $scope.response = response[0]
        },
        error: function (response) {
            console.log("could not retrieve list.");
            console.log(response.message);
        }
    });
});

我的HTML是

<body ng-app="myApp" class="text-center">
    <div id="foo" ng-controller="testController">
        {{response.title}}
    </div>
...
</body>

我可以在浏览器控制台中看到我确实在GET响应中获得了所需的内容

[Log] {userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit↵suscipit recusandae consequuntur …strum rerum est autem sunt rem eveniet architecto"} (javascript.js, line 27)

...但是文本未显示在呈现的HTML中。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

在这里回答我的后代问题。我从Ajax切换到此$ http方法。如您所知,我对此是超级全新的。如果有经验丰富的专家正在阅读本文,您可以在这里对两种方法进行评论,并描述我可能缺少的任何内容吗?

tSNE