Angular 使用ngfor inside div不显示restful api响应的结果
我有一个RESTful api,它提供如下结果集Angular 使用ngfor inside div不显示restful api响应的结果,angular,Angular,我有一个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 fi
{
"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;},
您的json似乎不是有效的,您能再次检查吗?this.movie是数组还是对象?当您调用此getMovies方法时?在ngOnInit或Constructor上。你能检查一下{movies | json}}的输出是什么吗HTML@diEchogetMovies在ngOnInit中,我在HTML上尝试了{movies | json}}同样的东西没有显示任何内容控制台中没有错误为什么使用response.json而不是response?完美的解决方案!!