0
我有一個角度的服務,需要照顧的加載項從服務器,然後當一個項目在視圖中被選中,相應的項目將被從項目列表中取出並返回到細節。JavaScript的承諾,緩存和角
的問題我現在是重新加載頁面時,我的項目還沒有加載,因此我的決心失敗,導致設備故障重裝。
getItems =(): ng.IPromise<Array<any>> => {
if (this.items != undefined) {
return this.items;
}
return this.$http.get(url).then((result) => {
this.items = result.data['items'];
return this.items;
});
}
getItem= (id: any): ng.IPromise<any> => {
return this.getItems().then(() => angular.forEach(this.items, (item: any) => {
if (item.id === id) {
return item;
}
}));
}
我認爲這段代碼(getItem)會返回一個單一的項目,但它會再次返回整個集合。我如何修復我的代碼讓我返回單個項目?
當你調用'getItem'功能? –
getItem從我的路由中的解析調用 – Patrick
在遞交給'forEach'的lambda中調用'return'沒有用,特別是它不會導致外部函數返回。 '... for'循環是你最好的選擇。 –