我试图从组件中获取数据到模板并循环它 .
我想要使用的模型
export class arsCodes {
iD: number;
aRSCode: string;
aRSDescription: string;
class: string;
victimFlag: string;
startDate: string;
endDate: string;
createdByUserID: string;
createdOnDateTime: string;
modifiedByUserID: string;
modifiedOnDateTime: string;
}
然后我没有得到错误就无法使用它
arsCodes: arsCodes[];
那么我用 any 的类型
referrals: any;
致电服务
this.arsSevice.getArsCodesApi(this.model)
.subscribe(
result => {
//this.arsCode = result
this.referrals = result;
console.log('component',result);
},
error => {
console.log(error);
}
)
component {Status:“success”,Message:“”,data:Array(16)}
最后我的模板HTML循环
<tr *ngFor="let ars of referrals; let i = index">
<td>{{i + 1}}</td>
<td>{{ars.arsCode}}</td>
<td>{{ars.description}}</td>
<td>{{ars.class}}</td>
<td>{{ars.startDate}}</td>
<td>{{ars.endDate}}</td>
</tr>
我希望数据能够通过console.log来实现
但我得到这个错误:
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
1 回答
看起来
arsCodes
是一个接口(https://angular-2-training-book.rangle.io/handout/features/interfaces.html):您应该导出
arsCodes
as有了这个,
arsCodes: arsCodes[];
应该工作 .在您的服务中,您可以使用
map
仅返回数组: