嘿,伙计们,我正在尝试从电影数据库api中获取数据,以便在我的Angular 5应用程序中显示出来。我设法让它通过JSONstringify在控制台中发布,但当我试图通过ngFor循环遍历数据时,它要么什么都不给我,要么对象,对象。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { Observable } from 'rxjs/Observable';
import { IMovie } from './movie';
@Injectable()
export class MoviesService {
private _url: string = "http://www.omdbapi.com/?i=tt3896198&apikey=*took-that-out*";
constructor(private http: HttpClient) {}
getMovies(): Observable<IMovie[]> {
return this.http.get<IMovie[]>(this._url);
}组件
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { MoviesService } from './../movies.service';
@Component({
selector: 'app-featured',
templateUrl: './featured.component.html',
styleUrls: ['./featured.component.css']
})
export class FeaturedComponent implements OnInit{
public movies = [];
constructor(private _moviesService: MoviesService){
}
ngOnInit() {
this._moviesService.getMovies()
.subscribe(data => this.movies = data);
}
}界面
export interface IMovie{
title: string;
year: string;
rated: string;
released: string;
runtime: string;
genre: string;
director: string;
writer: string;
actors: string;
plot: string;
language: string;
country: string;
awards: string;
poster: string;
//ratings: Ratings;
metascore: string;
imdbrating: string;
imdbVotes: string;
imdbid: string;
type: string;
dvd: string;
boxOffice: string;
production: string;
website: string;
response: string;
}和html
<h2>list of movies</h2>
<ul *ngFor="let movie of movies">
<li>{{movie.title}}</li>
</ul>编辑:从下面的api添加了JSON
{
"Title": "Guardians of the Galaxy Vol. 2",
"Year": "2017",
"Rated": "PG-13",
"Released": "05 May 2017",
"Runtime": "136 min",
"Genre": "Action, Adventure, Sci-Fi",
"Director": "James Gunn",
"Writer": "James Gunn, Dan Abnett (based on the Marvel comics by), Andy Lanning (based on the Marvel comics by), Steve Englehart (Star-lord created by), Steve Gan (Star-lord created by), Jim Starlin (Gamora and Drax created by), Stan Lee (Groot created by), Larry Lieber (Groot created by), Jack Kirby (Groot created by), Bill Mantlo (Rocket Raccoon created by), Keith Giffen (Rocket Raccoon created by), Steve Gerber (Howard the Duck created by), Val Mayerik (Howard the Duck created by)",
"Actors": "Chris Pratt, Zoe Saldana, Dave Bautista, Vin Diesel",
"Plot": "The Guardians must fight to keep their newfound family together as they unravel the mystery of Peter Quill's true parentage.",
"Language": "English",
"Country": "USA, New Zealand, Canada",
"Awards": "Nominated for 1 Oscar. Another 7 wins & 26 nominations.",
"Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BMTg2MzI1MTg3OF5BMl5BanBnXkFtZTgwNTU3NDA2MTI@._V1_SX300.jpg",
"Ratings": [
{
"Source": "Internet Movie Database",
"Value": "7.7/10"
},
{
"Source": "Rotten Tomatoes",
"Value": "83%"
},
{
"Source": "Metacritic",
"Value": "67/100"
}
],
"Metascore": "67",
"imdbRating": "7.7",
"imdbVotes": "336,851",
"imdbID": "tt3896198",
"Type": "movie",
"DVD": "22 Aug 2017",
"BoxOffice": "$389,804,217",
"Production": "Walt Disney Pictures",
"Website": "https://marvel.com/guardians",
"Response": "True"
}发布于 2018-10-11 00:24:49
获取[Object Object]是因为OMDb接口返回的对象有三个属性:{ Response,Search,totalResults }。
为了跳过这个错误,你只需要访问包含你的movies[]的搜索属性,并使用*ngFor遍历它。
代码示例:
<ul *ngIf="responseData">
<li *ngFor="let movie of responseData.Search">{{ movie.Title }}</li>
</ul>请记住检查您的{{ data binding }},并确保您的属性与响应obj属性匹配--它们都是大写的。
https://stackoverflow.com/questions/48811050
复制相似问题