Angular 9 找不到类型为“object”的不同支持对象“[object Object]”。NgFor 只支持绑定到 Iterables,比如 Arrays

Efr*_* A. 4 javascript arrays observable typescript-typings angular

我在模板中为 Observable 使用异步管道:

applicants$: Observable<UserProfile[]>;
  ngOnInit() {
    this.applicants$ = this.store.pipe(
      select(fromRootUserProfileState.getUserProfiles)
    ) as Observable<UserProfile[]>;
}
Run Code Online (Sandbox Code Playgroud)

这是 UserProfile.ts 界面:

import { Role } from './role/role';
import { Country } from './Country';

export interface UserProfile {
  id?: number;
  fullName?: string;
  roles?: Role[];
  windowsAccount?: string;
  firstName?: string;
  lastName?: string;
  email?: string;
  managerName?: string;
  managerId?: number;
  managerEmail?: string;
  companyId?: number;
  companyName?: string;
  countryId?: number;
  country?: Country;
  countryName?: string;
}
Run Code Online (Sandbox Code Playgroud)

这是 userProfile.service

  getUserProfiles(): Observable<UserProfile[]> {
    return this.http.get<UserProfile[]>(
      this.baseUrl + 'userprofiles/getuserprofiles'
    );
  }
Run Code Online (Sandbox Code Playgroud)

在模板中,我使用 ngFor 通过异步管道遍历 Observable:

<mat-form-field
  fxFlex="22"
  appearance="outline"
  *ngIf="applicants$ | async as applicants"
>
  <mat-label>Applicant</mat-label>
  <mat-icon matPrefix>person</mat-icon>
  <mat-select
    placeholder="Applicant"
    name="applicant"
    [(ngModel)]="this.searchPurchaseOrder.applicantId"
  >
    <mat-option *ngFor="let ap of applicants" [value]="ap.id">
      ap.fullName
    </mat-option>
  </mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

但是,这是我得到的错误: 在此处输入图片说明

这是来自商店的数据形状。它正在填充,所以那里没有问题: 在此处输入图片说明

Mat*_*ers 7

您将需要使用键值管道来循环遍历您的对象,就好像它是一个数组一样。

https://angular.io/api/common/KeyValuePipe

<div *ngFor="let applicant of applicants | keyvalue">
  {{applicant.key}}:{{applicant.value}}
</div>
Run Code Online (Sandbox Code Playgroud)

这也可以与异步管道一起使用,例如:

<div *ngFor="let item of (object$ | async) | keyvalue">
Run Code Online (Sandbox Code Playgroud)