2017-02-16 26 views
0

我正在使用omdbapi.com API爲電影數據製作一個簡單的搜索引擎。我已經設置了服務獲取數據和組件來創建視圖,但是當我嘗試連接到HTML我得到一個錯誤:如何在從Angular2的外部源獲取JSON之後顯示結果

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

電影類

export class Movie { 
    constructor(
     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, 
     Metascore: string, 
     imdbRating: string, 
     imdbVotes: string, 
     imdbID: string, 
     Type: string, 
     Response: string 
    ) {} 

} 

這裏我的組件:

import { Component, OnInit } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import { Subject } from 'rxjs/Subject'; 

//observable class extensions 
import 'rxjs/add/observable/of'; 
import 'rxjs/add/operator/switchMap'; 
import 'rxjs/add/operator/map'; 
//observable operators 
import 'rxjs/add/operator/catch'; 
import 'rxjs/add/operator/debounceTime'; 
import 'rxjs/add/operator/distinctUntilChanged'; 

import { MovieSearchService } from './movie-search.service'; 
import { Movie } from './movie'; 

@Component({ 
    selector: 'movie-search', 
    templateUrl: './movie-search.component.html', 
    styleUrls: ['./movie-search.component.css'], 
    providers: [MovieSearchService] 
}) 

export class MovieSearchComponent implements OnInit { 
    movies: Observable<Movie[]>; 
    private searchTerms = new Subject<string>(); 

    constructor(
     private movieSearchService: MovieSearchService, 
     private http: Http 
    ) {} 

    search(term:string) { 
     return this.searchTerms.next(term); 
    } 

    ngOnInit(): void { 
     this.movies = this.searchTerms 
     .debounceTime(300) // wait 300ms after each keystroke before considering the term 
     .distinctUntilChanged() // ignore if next search term is same as previous 
     .switchMap(term => term // switch to new observable each time the term changes 
     // return the http search observable 
     ? this.movieSearchService.search(term) 
      // or the observable of empty heroes if there was no search term 
     : Observable.of<Movie[]>([]) 
     ) 
     .catch(error => { 
      console.log("--------- Error -------"); 
      console.log(error); 

      return Observable.of<Movie[]>([]); 
     })  
    } 
} 

這是服務

import { Injectable } from '@angular/core'; 
import { Http, Jsonp } from '@angular/http'; 

import { Observable} from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 

import { Movie } from './movie'; 

@Injectable() 
export class MovieSearchService { 
    constructor(
     private http: Http, 
     private jsonp: Jsonp 
    ) {} 

    search(term: string): Observable<Movie[]> { 
     return this.http 
      .get(`http://www.omdbapi.com/?s=${term}`) 
      .map(response => { 
       return response.json().each() as Movie[] 
      }) 
    } 
} 

和視圖

<div class="col-xs-12"> 
    <input #searchBox id="search-box" /> 
    <button (click)="search(searchBox.value)">Search</button> 
</div> 

<ul *ngIf="movies"> 
    <li *ngFor="let movie of movies"> 
     {{ movie.title }} 
    </li> 
</ul> 

我如何可以使視圖顯示每部電影的片名? 預先感謝您的時間。

+0

可能重複[http與Observable在Angular 2不能使用數據](http://stackoverflow.com/questions/41961693/http-with-observable-in-angular-2-cant-use-data) – AngularChef

+0

請發佈API返回的JSON結構示例 –

回答

1

總是檢查網絡選項卡,看看您是否實際接收數據以及數據的外觀如何。

對此進行測試。顯然,你首先需要建立自己的網址是這樣的:

return this.http.get('http://www.omdbapi.com/?s='+term) 

然後作爲響應,它的建成是這樣的:

{"Search":[{"Title":"24","Year":"2001–2010","imdbID".... 

所以你需要解壓得到一個數組的內容:

.map(response => {response.json().Search}) 

,並訂閱你的組件:

this.movieSearchService.search(term) 
    .subscribe(d => this.movies = d) 

,然後當你要顯示你的標題:

<ul *ngIf="movies"> 
    <li *ngFor="let movie of movies"> 
     {{ movie.Title }} 
    </li> 
</ul> 

注意在上述代碼SearchTitle。這是區分大小寫的,因此您需要使用{{ movie.Title }}和大寫字母T來顯示數據。

這應該清理一下! :)

+0

感謝您的回覆@ AJT_82 –

+0

歡迎您! :)如上所述,使用您的開發人員工具,例如F12中的chrome。然後打開您的網絡選項卡並檢查請求並查看是否得到迴應。與您的網址有空的迴應,這就是爲什麼你不能迭代。這是一張照片。檢查我標記爲黃色的部分:) http://imgur.com/a/RuClo – Alex

+0

對不起,我在完成之前偶然發佈了我的答案。我得到一個錯誤:

Argument of type '(term: string) => Subscription | Observable' is not assignable to parameter of type '(value: string, index: number) => ObservableInput'. Type 'Subscription | Observable' is not assignable to type 'ObservableInput'. 
我相信這是因爲我宣佈電影作爲一個觀察的對象,如電影:可觀察 影片包含結構(名稱,年份等..) 如果我沒有聲明它是可觀察的,我應該如何聲明可變電影? –

相關問題