Javascript 为什么我得到的错误参数是未定义的?

Javascript 为什么我得到的错误参数是未定义的?,javascript,angularjs,angular-ui-grid,Javascript,Angularjs,Angular Ui Grid,在我的项目中,我使用ui网格过滤表 下面是javascript代码: (function () { "use strict"; angular.module("workPlan").controller("workPlanListController", ["workPlans", "clients", "inspectionAuthority", "$http", "config", "workP

在我的项目中,我使用ui网格过滤表

下面是javascript代码:

(function () {
    "use strict";

    angular.module("workPlan").controller("workPlanListController", ["workPlans",
        "clients",
        "inspectionAuthority",
        "$http",
        "config",
        "workPlanServise",
        workPlanListController
    ]);


    function workPlanListController(workPlans,
        clients,
        inspectionAuthority,
        $http,
        config,
        workPlanServise
    ) {
        var self = this;

        this.workPlanList = workPlans;
        this.lookups = {
            client: clients,
            authority: inspectionAuthority
        };

        this.gridOptions = {
            expandableRowTemplate: 'app/workPlan/templates/expandableRowTemplate.tmpl.html',
            expandableRowHeight: 150,
            enableColumnMenus: false,
            enableSorting: true,
            enableFiltering: false,
            enableToopTip: true,
            enableHorizontalScrollbar: 0,
            onRegisterApi: function (gridApi) {
                gridApi = gridApi,
                gridApi.expandable.on.rowExpandedStateChanged(null, function (row) {
                    if (row.isExpanded) {
                        row.entity.subGridOptions = {
                            enableColumnMenus: false,
                            enableSorting: true,
                            enableFiltering: false,
                            enableToopTip: true,
                            enableHorizontalScrollbar: 0,
                            columnDefs: [{ name: 'Frequency', field: 'inspectionFrequencyName', cellTooltip: true },
                                         { name: '# Sites', field: 'siteCount', cellTooltip: true },
                                         { name: 'Fixed Sites', field: 'normalSitesCount', cellTooltip: true }]
                        };
                        var arr = [];

                        workPlanServise.getSubGridContent(row.entity.clientId).then(function (result) {
                            row.entity.subGridOptions.data = result.data;

                            for (var i = 0; i < result.data.length ; i++) {
                                arr.push(
                                     {
                                         inspectionFrequencyName: row.entity[i].inspectionFrequencyName,
                                         items2inspect: row.entity[i].itemssiteCount2inspect,
                                         normalSitesCount: row.entity[i].normalSitesCount,
                                     });
                            }
                            row.entity.subGridOptions.data = arr;
                        });
                    }
                });
            }
        }

        this.gridOptions.columnDefs = [
        { name: 'Client', field: 'clientName', cellTooltip: true, headerTooltip: true },
        { name: '# Sites', field: 'siteNumbers', cellTooltip: true, headerTooltip: true },
        { name: '#FixedSites', field: 'siteNumbersInspected', cellTooltip: true, headerTooltip: true }];

        this.gridOptions.data = self.workPlanList;


        this.filterByClient = function () {
            gridApi.grid.refresh()
            singleFilter(self.gridApi.grid.rows, 'clientName', self.clientName);
        };


        this.singleFilter = function (renderableRows, field, filterValue) {
           var matcher = new RegExp(filterValue);
            renderableRows.forEach(function (row) {
               var match = false;

               if (row.entity[field].match(matcher)) { match = true; }

               if (!match) {
                   row.visible = false;
               }
           });
            return renderableRows;
        };
    }
})();
在这一行:

gridApi.grid.refresh()
我得到这个:错误:

Cannot read property grid of undefined
而在这种类似的情况下,它工作得很好


知道我为什么会出现上述错误吗?

您必须像示例一样将
gridApi
注入范围:

onRegisterApi: function(gridApi){
  $scope.gridApi = gridApi;
  // [...]
},
然后,您可以使用以下内容参考:

$scope.gridApi.grid.refresh();

在您的情况下,只需将
$scope
更改为

问题在于,
gridApi
参数只能在
onRegisterApi
函数中访问,除非您将其分配给其他变量

onRegisterApi: function (gridApi) {
    gridApi = gridApi, // Nothing happens by this line
}
你得这样试试

 var gridApi;
 this.gridOptions = {
            expandableRowTemplate: 'app/workPlan/templates/expandableRowTemplate.tmpl.html',
            expandableRowHeight: 150,
            enableColumnMenus: false,
            enableSorting: true,
            enableFiltering: false,
            enableToopTip: true,
            enableHorizontalScrollbar: 0,
            onRegisterApi: function (gridApiParam) {
               gridApi = gridApiParam, //Or assign the `gridApiParam` to `this` and access it the same way
               .......................
               .......................
            }
            .......................
            .......................
}

我是否必须这样做:onRegisterApi:function(gridApi){this.gridApi=gridApi;//[…]},是的,还有
this.gridApi.grid.refresh()
onRegisterApi: function (gridApi) {
    gridApi = gridApi, // Nothing happens by this line
}
 var gridApi;
 this.gridOptions = {
            expandableRowTemplate: 'app/workPlan/templates/expandableRowTemplate.tmpl.html',
            expandableRowHeight: 150,
            enableColumnMenus: false,
            enableSorting: true,
            enableFiltering: false,
            enableToopTip: true,
            enableHorizontalScrollbar: 0,
            onRegisterApi: function (gridApiParam) {
               gridApi = gridApiParam, //Or assign the `gridApiParam` to `this` and access it the same way
               .......................
               .......................
            }
            .......................
            .......................
}