Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'selection' of undefined

I'm using ui-grid. But I cannot get the $scope.gridApi from download function. And get the error: Cannot read property 'selection' of undefined. Somebody can tell me the reason? thanks.

    $scope.gridOptions.onRegisterApi = function (gridApi) {
        $log.info('gridApi...');
        $scope.gridApi = gridApi;
        $log.info($scope.gridApi);
    };
    $scope.download = function ($event) {
        $event.stopPropagation();
        var selectedRows = $scope.gridApi.selection.getSelectedRows();
        $log.log('selectedRows....');
        $log.log(selectedRows);
    };
like image 453
Charles Avatar asked Apr 20 '15 15:04

Charles


People also ask

How do you fix undefined properties Cannot be read?

To resolve your TypeError: Cannot read properties of undefined (reading '0') , go through these steps: Ensure you are using the correct variable. Perform a simple check on your variable before using it to make sure it is not undefined. Create a default value for the variable to use if it does happen to be undefined.

Can Read properties of undefined?

The "Cannot read properties of undefined" error occurs when trying to access a property on an undefined value. You often get undefined values when: accessing a property that does not exist on an object. accessing an index that is not present in an array.

Can not read the property of undefined Reading 0?

The "Cannot read property '0' of undefined" error occurs when trying to access the 0th index in a variable that stores an undefined value. Make sure to initialize the variable to the correct type, e.g. array or string, before accessing the index.

How do you fix TypeError Cannot read properties of null?

To solve the "Cannot read property 'value' of null" error, make sure you aren't accessing the value property on a null value, e.g. a non-existent DOM element. An element with the provided id does not exist in the DOM, so the getElementById method returns null .


Video Answer


2 Answers

There are three potential causes:

  • firstly, not including selection properly, either not including the module in your js code as a dependency, or not including the directive on your grid definition in your html. This will result in having a gridApi, but no gridApi.selection
  • secondly, trying to call this before onRegisterApi has fired, or defining onRegisterApi on your gridOptions after onRegisterApi has already fired. This will result in the method not being called, and therefore $scope.gridApi being undefined, which from your error text sounds like what the problem is.
  • lastly, problems with your invocation that means the $scope of your download call is different than the $scope you put the gridApi on. This shouldn't really happen, but I guess it could. You could tell this by putting a breakpoint in the method to see what $scope it has.
like image 130
PaulL Avatar answered Sep 18 '22 09:09

PaulL


I resolved this issue after checking:

  • ui-grid css loaded (ui-grid.min.css)
  • ui-grid javascript library loaded (ui-grid.min.js)
  • 'ui.grid' and 'ui.grid.selection' added to module initialization app = angular.module('app',['ui.grid','ui.grid.selection'])` )
  • adding the ui-grid-selection directive to the ui-grid markup
    <div id="my-grid" ui-grid="gridOptions" ui-grid-selection ></div>
like image 22
F.Igor Avatar answered Sep 19 '22 09:09

F.Igor