【发布时间】:2013-11-14 18:17:24
【问题描述】:
这样做的正确方法是什么?这是一个接一个的语句,但由于 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%' }
]
};
【问题讨论】:
-
如果存在依赖关系,为什么不在第一个 fn 中包含第二个 fn 调用?在有一个选定的项目之前,您不能填充详细信息网格,对吗? (如果我理解你的脚本 cmets)。
$scope.lastItem与填充网格的选定项目有什么关系? -
$scope.lastItem 是项目中的最后一项。第一次加载页面时,我希望它默认为表中的最后一项(最后一个 CongressId)
标签: c# javascript asp.net angularjs