【发布时间】:2019-07-05 12:31:19
【问题描述】:
我试图从内到外获得价值,然后使用承诺。
在我的控制器中,我有:
async Sample(userId: number)
{
let data = this.userService.getAffectedExpertisesPromise(userId);
await data.then((uri) =>
{
uri.forEach((exp: Expertise) =>
{
this.empList.push(exp.name);
});
})
return this.empList;
}
在ngOnInit上,我调用这个函数:
this.Sample(25).then(item =>
{
item.forEach((expLibelle: String) =>
{
listExp.push(expLibelle);
});
console.log("------------------------ List Size Inside: " + listExp.length);
});
console.log("------------------------ List Size Outside : " + listExp.length);
在服务用户文件中,我有:
getAffectedExpertisesPromise(id: number): Promise<any>
{
return this.http.get(`${this.baseUrl}/users/expertisesObj/${id}`).toPromise();
}
它产生:
------------ 列表大小外部:0
------------ 列表大小内部:3
如你所见:
-
then里面的大小是3 -->正确答案 -
then里面的大小为 0 --> 错误答案
你能帮我解决这个问题吗?
非常感谢。
【问题讨论】:
标签: angular6