【发布时间】:2018-11-07 17:22:53
【问题描述】:
我的 js 用于 Angular 从 http 获取 json 服务,并在我的 html 上使用 {{post.title}} 来获取数据并发布到我的 html。
数据未显示在 html 页面上 - 使用代码笔。
var app = angular.module("blogApp", []);
app.controller("mainCtrl", function($scope) {
$scope.posts = [];
let postsUrl ="https://jsonplaceholder.typicode.com/posts"
getPosts().then(posts =>{
$scope.posts = posts.slice(3);
$scope.$apply();
});
function getPosts(){
return fetch(postsUrl).then(res=>res.json());
}
});
【问题讨论】:
-
您可能还想添加相关的 HTML。就目前而言,我只能猜测这是因为您的代码集
$scope.posts(这是一个数组?),而不是$scope.post -
我们可以看到帖子的 JSON 和您的视图 html 吗?
-
我的 html 以: 开头,然后我尝试使用
- {{post.title}}
-
@mediaguru json 是服务jsonplaceholder.typicode.com/posts
-
当我使用 codepen 进行编码时,我在上面的 html 注释中省略了标题标签。