如何在 Angular 中从 Promise 获取数据



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

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

组件.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"
}
}

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

也许这就是你要找的:

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

最新更新