显示ngFor与json对象从服务返回

时间:2017-07-27 20:48:03

标签: angular

这是我的服务: https://api.myjson.com/bins/quv59

我试图在我的HTML中显示它

*ngFor="let item of booksList; let i = index"

我的ts档案

ngOnInit() {
  this.booksDataService.getBooks().subscribe(
  function(response) { 
    this.booksList = response.books;
  },
);}

提前致谢

3 个答案:

答案 0 :(得分:1)

  <li *ngFor="let item of booksList; let i = index">
    {{ item.author }} 
  </li>

答案 1 :(得分:0)

这是其他人帮助的正确答案...... 致电服务:

this.booksDataService.getBooks()
    .subscribe(
        allBooks => this.booksList = allBooks);}

HTML:

*ngFor="let item of booksList.books; let i = index"

答案 2 :(得分:0)

以下是使用异步管道的解决方案:

*ngFor="let item of booksDataService.getBooks() | async; let i = index"