我有一项服务正在本地获取书籍列表,但出现此错误:
BooksComponent.html:2 ERROR Error: Cannot find a differ supporting object
'[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.webpackJsonp.../../../common/@angular/common.es5.js.NgForOf.ngOnChanges (common.es5.js:1681)
at checkAndUpdateDirectiveInline (
这是我的服务类:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { Books } from './books';
@Injectable()
export class FetchbooksService {
books:any[];
constructor(private _http:Http) { }
fetch():Observable<Books[]>{
return this._http.get('./assets/books.json').map(
(response:Response)=> <Books[]>response.json(),
error => console.log(error)
).do(data => console.log(data));
}
}
这是组件:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { FetchbooksService } from '../fetchbooks.service';
import { Books } from '../books';
@Component({
selector: 'app-books',
templateUrl: './books.component.html',
styleUrls: ['./books.component.css']
})
export class BooksComponent implements OnInit {
bookss:Books[];
constructor(private _bookservice: FetchbooksService) {
_bookservice.fetch().subscribe(
(bookss)=> this.bookss = bookss,
error => console.log(error)=
);
}
ngOnInit() {
}
}
书籍是一个界面请告诉我我做错了什么?
这是 books.component.html :
<ul>
<li *ngFor = "let book of bookss ">
Book: {{book.title}}
</li>
</ul>
这是服务返回的数据:
console.log(bookss) 结果