首页 > 解决方案 > 如何使用角度应用程序中另一个对象的值创建一个对象以通过 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));
  }

有人可以告诉我如何使用上述代码为特定工作创建报价吗?

标签: angulartypescript

解决方案


您可以按如下方式进行。

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

}

推荐阅读