我有一個angularjs
服務來獲取我的json
數據。如何讓一個函數等待異步調用完成?
// Controller.js file
$scope.data=Events.query();
$scope.getEventtypes = function(){
angular.forEach($scope.data,function(value, key){
var arr = [];
if(key==$scope.month+$scope.year) {
for(var i=0; i<key.length; i++) {
arr.push(value[i].type);
$scope.eventtypes=arr.unique();
}
}
});
};
試圖訪問它拋出一個錯誤無法讀取的不確定財產type
的value[i].type
。我知道這是因爲異步調用。
//i want to add success function like this
$http.get('').success(function(data) {
....
});
任何人都可以幫助我?如果你建議做一些比這更有效的事情會更好。謝謝
我知道回調但不推薦,因爲它可以成爲嵌套回調的噩夢,如你所知。我們已經在我們的開發項目中使用了pull請求的代碼,因爲它看起來很乾淨並標記爲「合併好」;) – Mahbub