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)
您将需要使用键值管道来循环遍历您的对象,就好像它是一个数组一样。
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)
| 归档时间: |
|
| 查看次数: |
8568 次 |
| 最近记录: |