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%' }
    ]
};
4

1 回答 1

1

是的。你可以

//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;
    });
});
于 2013-11-14T18:23:24.177 回答