簡體   English   中英

如何使用 angular 應用程序中另一個對象的值創建一個對象以通過 HTTP POST 請求發送?

[英]How to create an object to send via HTTP POST request using values of another object in angular app?

在我的 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);
     });

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM