angular - Angular - 使用模型中的计算属性
问题描述
我们正在开发一个具有以下模型的应用程序:
interface IEmployee{
firstName?: string;
lastName?: string;
}
export class Employee implements IEmployee{
public firstName?: string;
public lastName?: string;
constructor(data: object | Employee) {
Object.assign(this, data);
}
get displayName(): string {
return this.firstName + ' ' + this.lastName;
}
}
我们有另一个模型 -Department
它引用了 Employee 类,如下所示:
interface IDepartment{
id?: string;
name?: string;
employees[]: Employee;
}
我们在项目的两个实例中进行了两次 API 调用——一个用于获取用户,另一个用于获取部门。在获取员工时,我们可以获取displayName
使用以下代码填充的员工:
this.http.get<Employee[]>(`${this.apiURL}\employees`)
.pipe(map(response => {
return response.map(data => new Employee(data));
}));
在获取部门时,有什么方法可以为所有员工填充员工显示名?
解决方案
您可以使用combineLatest,您可以在其中为输入定义 N 个可观察对象,例如:
combineLatest([departments$, employee$])
.pipe(
map(([departments, employee]) => /* do awesome staff */ )
);
如果两者都发出值,则 map 将执行
推荐阅读
- python-3.x - 如何使用可微张量流方法将张量分配给另一个张量的切片
- python - 发布数据有表单输入,但 Django modelform 没有保存它
- php - PHP如何将缺少的序列号查找到字母数字系列中
- python - 如何根据多列中的字符串匹配选择 Pandas 数据框中的行
- javascript - 如何将响应式 DIV 大小传递给 PHP?
- python - 如何从已经确定的峰/谷指数组中获得极值?[Python]
- mysql - 需要逻辑来为mysql中同一表中另一列的最小值找到一列的值
- android - 想要自定义带有提示的 EditText 属性
- excel - VBA 查找区分大小写的 DUPLICATE 行而不是单元格并删除
- .htaccess - .htaccess 检测桌面或移动设备并自动重定向