gpt4 book ai didi

javascript - Angular 9 找不到类型为 '[object Object]' 的不同支持对象 'object' 。 NgFor 仅支持绑定(bind)到 Iterables,例如数组

转载 作者:行者123 更新时间:2023-12-02 20:57:30 24 4
gpt4 key购买 nike

我在模板中使用异步管道来实现 Observable:

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

这是 UserProfile.ts 接口(interface):

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;
}

这是 userProfile.service

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

在模板中,我使用 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>

但是,这是我收到的错误: enter image description here

这是来自商店的数据形状。它正在填充,所以没有问题: enter image description here

最佳答案

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

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

<div *ngFor="let applicant of applicants | keyvalue">
{{applicant.key}}:{{applicant.value}}
</div>

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

<div *ngFor="let item of (object$ | async) | keyvalue">

关于javascript - Angular 9 找不到类型为 '[object Object]' 的不同支持对象 'object' 。 NgFor 仅支持绑定(bind)到 Iterables,例如数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61438176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com