角度4:找不到类型为“对象"的其他支持对象"[对象对象]". NgFor仅支持绑定到数组等Iterables [英] Angular 4 : Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays
本文介绍了角度4:找不到类型为“对象"的其他支持对象"[对象对象]". NgFor仅支持绑定到数组等Iterables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示从后端(CodeIgniter)到前端(Angular 4)的数据, 但是出现此错误:
I want to show the data from my backend (CodeIgniter) to my frontend (Angular 4), but this error come up:
错误错误:找不到类型>'对象'的其他支持对象'[对象对象]'. NgFor仅支持绑定到数组等Iterable.
ERROR Error: Cannot find a differ supporting object '[object Object]' of type > 'object'. NgFor only supports binding to Iterables such as Arrays.
这是我的组件:
import { BookService } from './../book.service';
import { Component, OnInit } from '@angular/core';
import { Book } from "../book";
@Component({
selector: 'app-book',
templateUrl: './book.component.html',
styleUrls: ['./book.component.css']
})
export class BookComponent implements OnInit {
constructor(public bookService: BookService ) { }
ngOnInit() {
this.getBooks();
}
books:Book;
getBooks(){
this.bookService.getBooks()
.subscribe(books=>{
this.books = books;
})
}
}
这是我的服务
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map' ;
@Injectable()
export class BookService {
constructor(private http: Http) {
}
books=[];
getBooks(){
return this.http.get("http://localhost/restserver/book/list_book.php")
.map(res => res.json());
}
}
我的.json
:
{"status":true,"data":
[{"id":"1","title":"SAINS","author":"ERLANGGA","isbn":"089928778"},
{"id":"2","title":"Geography","author":"ERLANGGA","isbn":"089182372"},
{"id":"3","title":"Math","author":"Srilangka","isbn":"091283181"},
{"id":"4","title":"test","author":"test","isbn":"1283798127"},
{"id":"5","title":"AAAA","author":"BBB","isbn":"91092301290"},
{"id":"6","title":"BBB","author":"CCC","isbn":"01920192"}]}
这是我的观点:
<div class="container-fluid">
<div class="row">
<div class="card col-md-7">
<div class="card-body">
<table class="table table-responsive table-striped">
<caption>List of Books</caption>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">ISBN</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let book of books" >
<th></th>
<td>{{book.title}}</td>
<td>{{book.author}}</td>
<td>{{book.isbn}}</td>
<td>
<button class="btn btn-primary "> Detail </button>
<button class="btn btn-success " [routerLink]="['/book-edit']"> Edit </button>
<button class="btn btn-danger "> Delete </button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<app-book-add class="col-md-5"></app-book-add>
推荐答案
问题:
您要将整个响应分配给书籍,但是您所需要的就是 数据.
You are assigning the whole response to books , but all you need is data.
1)第一种解决方法
在组件中更改此行:
this.books = books;
到
this.books = books.data;
2)或您也可以在模板中执行此操作:
更改此内容:
<tr *ngFor="let book of books" >
到
<tr *ngFor="let book of books?.data" >
这篇关于角度4:找不到类型为“对象"的其他支持对象"[对象对象]". NgFor仅支持绑定到数组等Iterables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文