无法读取属性' grid'模态中未定义的?

时间:2015-11-02 16:59:44

标签: javascript angularjs twitter-bootstrap

我有一个模式,我想要显示预先选定的行。但是,我不断阅读'无法读取网格'未定义的'错误。 UI网格在模态中定义,我声明两个Grid Apis有两个不同的名称。这是我的代码:

这将启动模式:

    //edit user group
    $scope.editSelectedGroup = function(){
        //get the selected row
        var row = $scope.gridApiInfo.selection.getSelectedRows();

        var modalInstance = $modal.open({
            animation: true,
            templateUrl: 'userGroupModal.html',
            controller: 'UserGroupModalController',
            resolve: {
                modalTitle: function () {
                    return "Edit User Group"
                },
                allDepartments: function () {
                    return $scope.allDepartments;
                },
                allRegions: function() {
                    return $scope.allRegions;
                },
                isEdit: function(){
                    return true;
                },
                row: function(){
                    return row[0];
                }
            }
        });

这是模态控制器:

    .controller('UserGroupModalController',function($scope,$modalInstance,$modal,$window,modalTitle,allDepartments,allRegions,isEdit,row,referenceDataService){

    $scope.modalTitle = modalTitle;
    $scope.isEdit = isEdit;
    $scope.allDepartments= allDepartments;
    $scope.allRegions = allRegions;

    $scope.form= {
        value: "",
        description: "",
        departments: [],
        regions: []
    };

    $scope.departmentsGrid = {
        enableRowSelection: true,
        multiSelect: true,
        enableRowHeaderSelection: false,
        onRegisterApi: function(gridApi) { 
            $scope.deptGridApi= gridApi;
        },
        columnDefs: [
            {name: 'Name', field: 'name'}
        ],
        data: $scope.allDepartments
    };

    $scope.regionsGrid = {
        enableRowSelection: true,
        multiSelect: true,
        enableRowHeaderSelection: false,
        onRegisterApi: function(gridApi) {
            $scope.gridApiRegions = gridApi;
        },
        columnDefs: [
            {name: 'Name', field: 'name'}
        ],
        data: $scope.allRegions
    };

    if ($scope.isEdit){
        $scope.form.value = row.value;
        $scope.form.description = row.description;

        //pushing selected depts
        angular.forEach(row.departments, function(department) {
            var deptElementPos=angular.findIndexOf($scope.allDepartments, department.id);
            $scope.form.departments.push($scope.allDepartments[deptElementPos]);
        });

        //pushing selected regions
        angular.forEach(row.regions, function(region) {
            var regionElementPos=angular.findIndexOf($scope.allRegions, region.id);
            $scope.form.regions.push($scope.allRegions[regionElementPos]);
        });

        //setting pre-selected rows
        angular.forEach($scope.form.departments, function(department) {
            $scope.deptGridApi.grid.rows.map(function (row) {
                if (row.entity.id == department.id) {
                    row.setSelected(true);
                    $log.log("row selected: " + row.entity.id);
                }
            });
        });

        angular.forEach($scope.form.regions, function(region) {
            $scope.gridApiRegions.grid.rows.map(function (row) {
                if (row.entity.id == region.id) {
                    row.setSelected(true);
                    $log.log("row selected region: " + row.entity.id);
                }
            });
        });

        $scope.form.id = row.id;
    }

    $scope.close = function () {
        $modalInstance.dismiss();
    };
})

当我点击按钮启动模态时,没有模态显示 - 而是我收到控制台错误,说它“无法读取属性'网格'未定义的'在$ scope.deptGridApi.grid.rows.map的行上。任何人有任何建议如何解决这个问题?提前谢谢!

编辑:使用deptGridApi和gridApiRegions获取所选行 - 我编写了一个通过单击模态中的按钮激活的测试函数,如下所示:

        $scope.getDeptandRegions= function(form){
            $log.log($scope.gridApiRegions.selection.getSelectedRows());
            $log.log($scope.deptGridApi.selection.getSelectedRows())  
         };

即使这也使用网格API,这些也会很好地记录所选行。这是按下按钮后才会被解雇的吗?

1 个答案:

答案 0 :(得分:1)

Cannot read property 'grid' of undefined' at the line with $scope.deptGridApi.grid.rows.map

表示:

$scope.deptGridApi === undefined

查看您的代码是因为onRegisterApi

  • 从未打过电话
  • 以“undefined”作为参数调用

如果您可以提供有效的代码段,我可以帮助您更好地