如何从Angular中的Promise获取数据

时间:2019-10-14 10:58:57

标签: angular typescript

我正在尝试执行Promise后访问作为响应返回的数据。以json形式返回的响应是一个哈希表。我需要访问相同的键和值。不同的键及其对应的值需要在每个对话框中显示:-

我已经尝试以以下方式访问数据:-

component.ts:

openDialog(emaildialog){
   this.emailservice.getByEmailType(this.choice).then((emails: any) => {
      this.etext = emails.responsive;
      console.log(emails.responsive);
      let ref = this.dialog.open(emaildialog, {    
         data: emails.responsive,
         width: "600px",
         height: "600px",
      });
   });
}

service.ts:

constructor(private http: HttpClient) { }

getByEmailType(id:String) {
   return this.http.get<Email>(this.baseUrl+'/'+id).toPromise();
}

在后端作为Srpingboot,api返回以下数据:

return new ResponseEntity<Object>(Collections.singletonMap("responsive",hmap), HttpStatus.OK);

返回的json如下:

{
   "responsive": {
      "email3": "hello to email3",
      "email2": "hello to email2",
      "email1": "hello to email1",
      "email5": "hello to email5",
      "email4": "hello to email4"
   }
}

我需要访问每种电子邮件类型作为键和相应的值。我需要在每种电子邮件类型的单独对话框中显示它们。

1 个答案:

答案 0 :(得分:0)

也许这就是您想要的:

Object.keys(emails.responsive).forEach(e => {
      let ref = this.dialog.open(emaildialog, {    
         data: emails.responsive[e],
         width: "600px",
         height: "600px",
      });
});