【发布时间】:2018-07-12 09:12:20
【问题描述】:
为什么this.menulist 之后还是空的?
layout.component.ts
ngOnInit() {
this.menulist = localStorage.getItem('menulist');
if (this.menulist) {
} else {
this.SetMenuList(); // Else call the service and sort the menu then
}
this.menulist = localStorage.getItem('menulist'); // null even after calling SetMenulist
this.jsonmenulist = JSON.parse(this.menulist);
}
SetMenuList() {
this._UserspecificmenuaccessService.getRMA("driver")
.map((lst) => {
if (lst && lst.length > 0) {
console.log(lst);
localStorage.setItem('menulist', JSON.stringify(lst));
this.menulist = localStorage.getItem('menulist'); this.jsonmenulist = JSON.parse(this.menulist);
}
}, (error) => {
console.error(error)
});
}
服务方式:
getRMA(Id:any): Observable<Imstmenu[]> {
return this._http.get("http://172.19.32.235:3000/api/USMA/selectAll/"+Id+"")
.map((response: Response) => {<Imstmenu[]>response.json();
.catch(this.handleError);
}
【问题讨论】:
-
你写 lst 来记录。它显示了什么?
-
SetMenuList()是一个异步函数,运行后的行并不意味着SetMenuList()结束运行。 -
@Programmer 它只是没有出现......就像执行流程从未到达它。
-
@ibenjelloun 那我该怎么办?
标签: javascript angular angular5 angular2-services angular4-forms