2013-11-14 44 views
0

這樣做的正確方法是什麼?這裏是一個接一個的聲明,但由於JavaScript是異步我猜測$ score.lastItem不存在,當第二個函數被調用?那麼我是否將第二個函數放在第一個回調函數中?在調用方法填充細節網格之前,如何從填充的下拉列表中獲取信息?

//1st Call this on inital load to populate Congresses dropdownlist 
ccResource.query(function (data) { 
    $scope.ccList.length = 0; 
    angular.forEach(data, function (ccData) { 
     $scope.ccList.push(ccData); 
    }) 

    $scope.lastItem = $scope.ccList[$scope.ccList.length - 1]; 
}); 

//2nd after populating $scope.lastItem run this to populate grid on initial load (using id from selected item in dropdownlist) 
cgResource.query({ id: $scope.lastItem.congressNumber }, function (data) { 
    $scope.usersList = data; 
}); 

//ngGrid 
$scope.userGrid = { 
    data: 'usersList', 
    multiSelect: false, 
    selectedItems: $scope.selectedUsers, 
    enableColumnResize: false, 
    columnDefs: [ 
     { field: 'firstname', displayName: 'First Name', width: '25%' }, 
     { field: 'lastname', displayName: 'Last Name', width: '25%' } 
    ] 
}; 
+0

如果there'a的依賴,爲什麼不包括在第一FN第二FN電話嗎?在選定的項目正確之前,您無法填充詳細信息網格? (如果我理解你的腳本評論)。 '$ scope.lastItem'與填充網格的選定項目有什麼關係? –

+0

$ scope.lastItem是項目中的最後一個項目。當頁面第一次加載時,我希望它默認爲表格中的最後一個項目(last CongressId) – punkouter

回答

1

Yes.You可以做

//1st Call this on inital load to populate Congresses dropdownlist 
ccResource.query(function (data) { 
    $scope.ccList.length = 0; 
    angular.forEach(data, function (ccData) { 
     $scope.ccList.push(ccData); 
    }) 

    $scope.lastItem = $scope.ccList[$scope.ccList.length - 1]; 

    //2nd after populating $scope.lastItem run this to populate grid on initial load (using id from selected item in dropdownlist) 
    cgResource.query({ 
     id: $scope.lastItem.congressNumber 
    }, function (data) { 
     $scope.usersList = data; 
    }); 
}); 
+0

謝謝..由於某些原因,我無法從C#樣式到JavaScript函數函數風格的函數...我想這被稱爲'承諾'? – punkouter