【问题标题】:How do get information from a populated dropdownlist before calling a method to fill the details grid?在调用方法填充详细信息网格之前,如何从填充的下拉列表中获取信息?
【发布时间】: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


【解决方案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;
    });
});

【讨论】:

  • 谢谢.. 出于某种原因,我在函数风格的函数中从 C# 风格转换为 Javascripts 函数时遇到了麻烦......我猜这被称为“承诺”?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多