[英]Angular 2 *ngFor Error
I am getting this error "find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays." 我收到此错误“找到'对象'类型的不同支持对象'[object Object]'。NgFor仅支持绑定到诸如Arrays之类的Iterables。”
here is my code: 这是我的代码:
<tbody>
<tr *ngFor='let tab of tabs'>
<td>
<ul class="list-unstyled list">
<li><a href="#" class="anchorLink"><i class="icon-home scolor"></i><font color="white">{{tab.TabName}}</font></a></li>
</ul>
</td>
</tr>
</tbody>
navMenu.copnent.ts navMenu.copnent.ts
import { Component, OnInit } from '@angular/core';
import { NavMenuService } from './navMenu.service';
import { InavMenuTabs } from './navMenu';
@Component({
selector: 'nav-menu',
moduleId: module.id,
templateUrl: 'navMenu.component.html'
})
export class NavMenuComponent implements OnInit {
tabs: InavMenuTabs[];
errorMessage: string;
constructor(private _navMenuService: NavMenuService) {
}
ngOnInit(): void {
this._navMenuService.getTabs(1, 'XXXX')
.subscribe(tabs => this.tabs = tabs,
error => this.errorMessage = <any>error);
}
}
navMenu.service navMenu.service
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { InavMenuTabs } from './navMenu';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
@Injectable()
export class NavMenuService {
private _fileUploadAPI = 'http://localhost:50180/API/FileUpload/GetTabs/';
constructor(private _http: Http) {
}
getTabs(LinkID: number, PSNL_UID: string): Observable<InavMenuTabs[]> {
return this._http.get(this._fileUploadAPI + LinkID.toString() + '/' + PSNL_UID)
.map((response: Response) => <InavMenuTabs[]>response.json())
.do(data => console.log('All' + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server Error');
}
}
navMenu.ts navMenu.ts
export interface InavMenuTabs {
LinkTabID: number;
TabName: string;
}
Well, looking at your comments, that explains it. 好吧,看看你的评论,这就解释了。 Your incoming data is not an Array, but an object named
result
, so you have to get the content from that object and store it in your array. 传入的数据不是数组,而是名为
result
的对象,因此您必须从该对象获取内容并将其存储在数组中。
When you mapped your incoming data it showed: 当您映射传入数据时,它显示:
{"result":[{"LinkTabID":1,"TabName":"Upload File"}]}
which is an object containing an array. 这是一个包含数组的对象。 So you have to get that array from your object.
所以你必须从你的对象中获取该数组。
ngOnInit(): void {
this._navMenuService.getTabs(1, 'XXXX')
.subscribe(
data => {
this.tabs = data.result
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.