angular - 在 div 中使用 ngfor 不显示 restful api 响应的结果
问题描述
我有 RESTful api,它提供了类似的结果集
{
"count": 10,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"title": "Rocky (1976)",
"description": "A small-time boxer gets a supremely rare chance to fight a heavy-weight champion in a bout in which he strives to go the distance for his self-respect.",
"moviePoster": "http://127.0.0.1:8000/pic_folder/no-poster.jpeg",
"avg_rating": 5,
"no_of_ratings": 1,
"maxRatings": 5
},
{
"id": 2,
"title": "Rocky (1976)",
"description": "A small-time boxer gets a supremely rare chance to fight a heavy-weight champion in a bout in which he strives to go the distance for his self-respect.",
"moviePoster": "http://127.0.0.1:8000/pic_folder/no-poster.jpeg",
"avg_rating": 3.5,
"no_of_ratings": 2,
"maxRatings": 5
}
]
}
在 component.ts 我有以下方法:
getMovies() {
this.movieService.getMovies().subscribe(
response => {
this.movies= response.JSON;
console.log('response.JSON',response);
},
error => {
this.snackBar.open('Error getting Movies', '', { duration: 3000 });
}
);
}
我正在尝试在 component.html 中打印“电影标题”,并且我关注了 div,但 html 页面上没有打印任何内容。
<div class="container">
<h2>Movies:</h2>
<div *ngFor="let movie of movies;" >
<h4>{{movie.title}}</h4>
</div>
解决方案
做这个改变
this.movieService
.getMovies()
.subscribe( (response) => { this.movies = response.results;},
推荐阅读
- java - IntelliJ IDEA 的 Checkstyle 插件
- c# - Lucene.Net 跨文档中存在的多个字段搜索多个术语
- angular - Trying to filter an Observable but property doesn't exist on type
- python - Suspected order of functions/while loop glitches game out
- google-cloud-speech - Google Speech API - 数字表示为字符串?
- sql - 如何提高我的查询的性能
- php - preg_match_all 负前瞻和负后瞻
- git - Search for a text accross all revision of a file
- javascript - 有没有办法将变量从程序内存存储到硬盘保留函数
- android - 如何生成兼容大多数版本的apk(SDK)