我目前正在学习 Angular 2 对不起,如果我太菜了,但我似乎无法使用多个链异步函数......
我将尝试解释和缩写我的代码,我正在向 API 请求一个 get 方法,它是一个员工列表,员工列表必须显示在一个当前工作正常的表中,该表称为“listToDisplay”,但是问题是当日历显示时,它没有显示我在 listEmployeeBirthday 中填写的任何生日
我发现当我调用 updateCalendar 方法时,系统不会等待 listEmployeeBirthday 被填写,正如我在代码中所示,我控制台记录了 listEmployeeBirthday 并且它正确填写但出现警告消息:“下面的值已评估现在”。我已经尝试了一切来解决这个问题,但我似乎没有得到在 getEmployees 完成后执行 updateCalendar 的工作。
我将不胜感激任何帮助
ngOnInit(){
this.getEmployees()
this.updateCalendar()
console.log("listEmployeeBirthday")
}
getEmployees(){
this.http.get("apiadress...")
.flatMap((response:Response) => response.json());
.map((employee)=>{
this.addCalendar(employee);
this.listToDisplay.push(employee)
})
.subscribe();
}
addCalendar(employee){
this.listEmployeeBirthday.push(employee.birthdate);
}
updateCalendar(){
methodToUpdateCalendar(this.listEmployeeBirthday)
}
是的,您的updateCalendar
函数不执行任何操作,因为它getEmployees
是异步的。所以你需要在员工加载时更新日历:
this.http.get("apiadress...")
.flatMap((response:Response) => response.json())
.map((employee)=>{
this.addCalendar(employee);
this.listToDisplay.push(employee);
})
.subscribe(() => {
this.updateCalendar();
});
顺便说一句,map
运算符应该用于实际映射数据,而不是用于执行某些操作。更好的方法是:
this.http.get("apiadress...")
.flatMap((response:Response) => response.json())
.subscribe(() => {
this.addCalendar(employee);
this.listToDisplay.push(employee);
this.updateCalendar();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句