如何在角度服务中编写帖子

时间:2019-07-17 02:57:34

标签: angular typescript http angular7 angular-services

我有一项服务,其输入如下,

{
  "overviewPreferences": {
    "selectedRole": [{
      "code": "PRACTITIONER"
    }],
    "selectedDomainList": [{
      "code": "2",
      "name": "HR"
    },
    {
      "code": "3",
      "name": "PAYROLL"
    }]
  }
}

我的接口声明如下,

export interface UserDomain {
  code: string;
  value: string;
  selected: boolean;
}

我的服务

updateUserSelectedDomains(code: String): Observable<any> {
  return this.http.post<UserDomain>(`/adpi/rest/core/v2/business-intelligence/workers/${aoId}/preferences") `, request)
    .pipe(
      map(data => this.data = data),
    );  
}

我想知道接口应该是什么?是我发送的请求还是得到的响应。我不确定我是否正确执行了。任何人都可以查看我的代码。谢谢。

2 个答案:

答案 0 :(得分:0)

post<ResponseType>

严格键入返回的observable。因此,调用此方法将返回一个Observable,因此当您订阅该Observable时,观察者将收到一个ResponseType对象。

答案 1 :(得分:0)

 // pass postbody object from your component to service method, so service will read 
 // parameter value in reqBody .
 //Then it will pass to post call API 

 let postBody = {
        "overviewPreferences": {
            "selectedRole": [{
                "code": "PRACTITIONER"
            }],
            "selectedDomainList": [{
                "code": "2",
                "name": "HR"
            },
            {
                "code": "3",
                "name": "PAYROLL"
            }]
          }
 } 

servicename.updateUserSelectedDomains(postBody).subscribe( (response)=>{
       console.log(response);
 })




// =============== Service code ====================
updateUserSelectedDomains(reqBody): Observable<any> {
   return this.http.post<UserDomain>(`/adpi/rest/core/v2/business- 
   intelligence/workers/${aoId}/preferences" `, reqBody)                    
}