Angular 绑定到对象而不是 ArrayList

Angular binding to an Object not ArrayList

我正在使用 Angular 并尝试遍历一个对象

JSON

employee {"fName":"mike","email":"XXXXXXXX@hotmail.com"}

界面

export interface Employee {
fName:string
email:string

}

分量

    iEmployee: Employee[];
getITIMDATA() {
        this._ersaDet.getITIMData(this.ersaGlobalUID)
            .subscribe(
            data => {
                this.iEmployee = data;
            },
            error => 'GetAllCostCenetrs Method: ');

    }

HTML

 <div *ngFor="let itim of iEmployee">
        <div class="col-lg-12">
            <div class="row">
                <div class="form-group">
                    <label class="control-label">Name</label>
                    <input type="text"  name="Name" class="form-control" id="ntName" [(ngModel)]="itim.fName">
                </div>
            </div>
        </div>
 <div>

我在执行代码时得到的错误是

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

嗯,你的订阅码是

data => {
    this.iEmployee = data;
}

data的内容是什么?你能在赋值后打印 this.iEmplyoee 的值吗?

TypeScript 编译器说它不是对象数组。

你能创建一个简单的工作示例吗? (在 Stackblitz、plunkr、....)

更新

您似乎希望收到一条记录而不是一个数组,因此您可以:

  • 删除 ngFor
  • 将订阅中的代码更改为类似

    的内容

    数据 => { this.iEmployee = [数据]; }

顺便说一句,当您尝试将单个对象分配给对象数组时,您没有从编译器获得 error/warning,这很奇怪。 您是否删除了 tsconfig 文件中的某些检查?

您应该 return json 中的数组而不是单个对象。即使你只有一个对象,你也应该 return 一个由单个对象组成的数组。