angular - 使用对象参数拉另一个对象
问题描述
重新向 Angular 介绍自己,想问一个问题。我将约会数据提取到我的字段中,我需要使用约会对象中的“UserId”参数提取用户名,并使用用户中的“UserId”参数提取用户,然后显示名称. 它们都可以很好地进入我的控制台,但是我该如何在前端编写它来做到这一点?
<mat-accordion>
<mat-expansion-panel class="content" *ngFor="let app of appointments, let i = index">
<mat-expansion-panel-header *ngFor="let user of users">
<mat-panel-title>Image: </mat-panel-title>
<mat-panel-title ***ngIf="app.UserId === user?.UserId">Client: {{user?.ContactId}}**</mat-panel-title>
<mat-panel-title>Location: {{this.currentLocation}}</mat-panel-title>
<mat-panel-title>Date: {{app.StartDate| date : 'MM-dd-yyyy'}}</mat-panel-title>
<mat-panel-title>Time: {{app.StartDate| date : 'hh:mm'}}</mat-panel-title>
<i class="material-icons-outlined" style="align-self: end">expand_more</i>
</mat-expansion-panel-header>
<mat-panel-description>
<p>Desc: {{app.Description}}</p>
</mat-panel-description>
</mat-expansion-panel>
</mat-accordion>
解决方案
您的目标是pipe
过滤您的数据。让我们通过一个简单的例子。
Apipe
看起来像这样filter-user.pipe.ts:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filterUser',
pure: false
})
export class FilterUserPipe implements PipeTransform {
transform(users: string[], filterUser: string): any {
if (!users || !filterUser) {
return users;
}
return users.filter(user => user === filterUser);
}
}
您component
的约会和用户数据app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
styleUrls: ['./app.component.scss'],
templateUrl: './app.component.html'
})
export class AppComponent {
// your Appointments
appointments: Appointment[] = [
{
name: 'Brolly',
surname: 'Patel',
date: new Date()
},
{
name: 'Goku',
surname: 'Smith',
date: new Date()
},
{
name: 'jimmy',
surname: 'Smith',
date: new Date()
}
];
// your Users
users: string[] = ['John', 'Brolly', 'Goku', 'Gohan'];
}
// Interface you may want to put in another separate file.
export interface Appointment {
name: string;
surname: string;
date: Date;
}
为了方便您理解,我呈现了以下 HTML app.component.html:
<div *ngFor="let appointment of appointments">
<div *ngFor="let user of users | filterUser: appointment.name">
{{user}}
</div>
</div>
当您创建时,请pipe
记住将其添加到您module
的声明数组中。(我说的是关于模块,因为它有助于延迟加载)app.module.ts:
import { AppComponent } from './app.component';
import { CoreModule } from './core';
import { FilterUserPipe } from './filter-user.pipe';
import { HelloFrameworkModule } from './hello-framework';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent, FilterUserPipe],
imports: [BrowserModule, CoreModule, HelloFrameworkModule]
})
export class AppModule {}
您的输出将如下所示:
Brolly
Goku
因此,在您的代码示例中,这就是它的外观:
// Your HTML would roughly look like this:
<mat-accordion>
<mat-expansion-panel class="content" *ngFor="let app of appointments, let i = index">
<mat-expansion-panel-header *ngFor="let user of users | filterUser: app.UserId">
<mat-panel-title>Image: </mat-panel-title>
<mat-panel-title>Client: {{user?.ContactId}}**</mat-panel-title>
<mat-panel-title>Location: {{this.currentLocation}}</mat-panel-title>
<mat-panel-title>Date: {{app.StartDate| date : 'MM-dd-yyyy'}}</mat-panel-title>
<mat-panel-title>Time: {{app.StartDate| date : 'hh:mm'}}</mat-panel-title>
<i class="material-icons-outlined" style="align-self: end">expand_more</i>
</mat-expansion-panel-header>
<mat-panel-description>
<p>Desc: {{app.Description}}</p>
</mat-panel-description>
</mat-expansion-panel>
</mat-accordion>
// Your pipe would need modifying slightly as your using an object:
export class FilterUserPipe implements PipeTransform {
transform(users: string[], filterUserId: string): any {
if (!users || !filterUserId) {
return users;
}
return users.filter(user => user.UserId === filterUserId);
}
}
推荐阅读
- angular - 如何在 Angular 服务中本地保存 Apollo 响应?
- android - Kotlin 捕获图像并将其存储在本地
- postgresql - PostgreSQL 为有序查询选择了错误的多列索引
- redis - 如果我们在 Key 上进行分区,为什么我们需要在 Redis 中使用分布式锁?
- nlp - 使用 SageMaker BlazingText (word2vec) 进行预训练/迁移学习?
- python - ModuleNotFoundError: 没有使用 celery 和 rabbitmq 的名为 'django.config' 的模块
- c++ - 对预先分配的工作区使用 mutable
- java - Hibernate ORM 5.4 并删除了统计信息?
- python - 如何以年、月和日计算年龄?
- javascript - Socket.io 的路径在路径末尾添加斜线