Angular 7:NgFor仅支持绑定到可迭代对象,例如数组

时间:2019-04-12 05:36:32

标签: angular7

我试图在显示屏上显示响应,但始终在角度7中出现此错误。我试图检查其他解决方案,但没有帮助。     ExpensesComponent.html:2错误错误:找不到其他内容     类型为“对象”的支持对象“ [对象对象]”。仅NgFor     支持绑定到数组等Iterable。

这是在控制台中对我的请求的响应。 内容:Array(4)

class dashboardViewController: UIViewController {

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
        case 0:
            subTitleLabel.text = "Temperature"
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController                
            self.view.insertSubview(viewController.view!, belowSubview: self.dashboardTabBar)
}
}

此处是应将这些响应设置为局部变量的函数。

0: {id: 2, description: "LED TV", expenseDate: "2019-04- 
   11T18:00:00.000+0000", expenseAmount: 2000}
    1: {id: 3, description: "LED TV 2", expenseDate: "2019-04- 
    11T18:00:00.000+0000", expenseAmount: 2000}
    2: {id: 4, description: "LED TV 3", expenseDate: "2019-04- 
    11T18:00:00.000+0000", expenseAmount: 13000}
    3: {id: 5, description: "LED TV 4", expenseDate: "2019-04- 
    11T18:00:00.000+0000", expenseAmount: 14000}

这是费用类别。

expenseList : Expense[];
refreshExpensePage(){
   this.expenseDataService.retriveAllExpenses(this.page).subscribe(
   res=>{console.log(res); 
    console.log(res);
     this.expenseList = res; 
  }
 ); 

这是应该在表中显示数据的html。

 export class Expense{
  constructor(
  public id:number,
  public description : string,
  public expenseDate: Date,
  public expenseAmount: number
    ){
   }
 } 

1 个答案:

答案 0 :(得分:0)

与其使用构造函数创建类,不如尝试定义为接口以定义对象。

export interface Person {
 id:number;
 description : string;
 expenseDate: any;
 expenseAmount: number;
}

并在组件中导入接口(以防接口在其他文件中定义),然后将接口声明为默认数据类型

expenseList : Person[];

然后在 expenseList

中分配适当的对象数组

一切顺利