angular - 如何使用角度应用程序中另一个对象的值创建一个对象以通过 HTTP POST 请求发送?
问题描述
在我的 Angular 应用程序中,我有一个Job
对象和一个Offer
对象。
这是我的接口:
export interface IJob {
id: number;
title: string;
description: string;
employeeId?: number;
managerId: string;
imageUrl: string;
}
export interface IOffer {
id: number;
managerId: number;
jobId: number;
employeeId: number;
}
我正在显示所有作业详细信息,如下所示:
<table">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Employee ID</th>
<th scope="col">Manager ID</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let job of jobs">
<td>{{ job.title }}</td>
<td>{{job.description}}</td>
<td>{{job.employeeId}}</td>
<td>{{job.managerId}}</td>
<td>
<button (click)="applyForJob(job)">Apply Now</button>
</td>
</tr>
</tbody>
</table>
我想使用此applyForJob(job)
方法为关联的作业创建一个Offer
对象。
我目前在我的优惠服务中有这种方法,如果有帮助的话:
addOffer(offer: IOffer): Observable<IOffer> {
return this.httpClient.post<IOffer>(this.baseUrl, offer, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
})
.pipe(catchError(this.handleError));
}
有人可以告诉我如何使用上述代码为特定工作创建报价吗?
解决方案
您可以按如下方式进行。
applyForJob(job) {
let offer = new IOffer();
offer.id = job.id; // use unique id for this
offer.managerId = job.managerId;
offer.jobId = job.id;
offer.employeeId = job.employeeId;
myOfficeService.addOffer(offer).subscribe((res: any) => {
console.log(res);
});
}
推荐阅读
- d3.js - d3-drag - “无法读取 null 的属性‘按钮’”
- azure-active-directory - 将 Microsoft Graph 访问权限锁定到一个域
- python - 如何在python中将多个if else循环转换为列表理解
- solr - solr函数查询示例
- php - values added as blank spaces php sql
- python - 用于从地理点列表中查找最近位置对的 Python 脚本
- wpf - Can't get Dictionary<> (Or ObservableCollection
) to bind to ListView - java - 移动到新活动时,Android Studio 保存 radiogroup 值
- c# - 无法在视图中显示名称列表
- mapbox - 使用mapbox为每个多边形动态着色?