我正在使用ui-grid和306_expandable_grid,我已经使用了doc中的确切代码,但我遇到了一个错误问题.
app.js
$http.get('resources/data/title2.json') .success(function(data){ console.log(data); console.log(data[0].Data); console.log(data.length); for(i = 0; i < data[i].length; i++){ data[i].subGridOptions = { columnDefs: [ {name:"Title" },{name:"Jan-10" },{name:"Feb-10"},{name:"Mar-10" },{name:"Apr-10" },{name:"May-10" },{name:"Jun-10" },{name:"Jul-10" },{name:"Aug-10" },{name:"Sep-10" },{name:"Oct-10" },{name:"Nov-10" },{name:"Dec-10" } ],data: data[i].Data } } // $scope.subGridOptions.data = data; $scope.gridOptions.data = data; // $scope.title = data[0].Title; });
externalHtmlFile.html
<div ui-grid="row.entity.subGridOptions" style="height:150px;"></div>
这是我坚持的错误
TypeError: Cannot read property 'data' of undefined at new <anonymous> (http://localhost/ng-Grid/resources/scripts/ui-grid-unstable.js:2883:41) at Object.e [as invoke] (http://localhost/ng-Grid/resources/scripts/angular.min.js:36:456) at E.instance (http://localhost/ng-Grid/resources/scripts/angular.min.js:75:118) at http://localhost/ng-Grid/resources/scripts/angular.min.js:58:276 at r (http://localhost/ng-Grid/resources/scripts/angular.min.js:7:408) at M (http://localhost/ng-Grid/resources/scripts/angular.min.js:58:260) at http://localhost/ng-Grid/resources/scripts/angular.min.js:65:412 at http://localhost/ng-Grid/resources/scripts/angular.min.js:109:276 at h.$eval (http://localhost/ng-Grid/resources/scripts/angular.min.js:123:139) at h.$digest (http://localhost/ng-Grid/resources/scripts/angular.min.js:120:220)
解决方法
首先,确保在$http.get成功回调之外初始化$scope.gridOptions.只在成功回调中将$scope.gridOptions.data [i] .subGridOptions添加到$scope.gridOptions.
Reference:
You can’t define the grid options in the success call. You need to define them on the scope in your controller and then set the data or column definitions,etc… from the success call.
其次,确保网格/页面的控制器没有任何类型的初始化参数相关的重定向.由于我在页面上的以下代码,我遇到了生成的相同错误消息:
if (typeof clientcode === 'string' && clientcode !== '') { var payload = { clientcode : storedClientcode } } else { $location.path('/'); }
这里的想法是检查storedClientcode是否由调用页面(通过共享应用程序根服务)提供,如果没有,则重定向到应用程序主目录.但是ui-grid明显地对页面进行了单独的子调用,而那些子调用当然不提供我用我的代码寻找的数据,因此重定向发生在幕后的那些调用中.没有出现任何错误(除了您也看到过的错误),并且没有数据显示在网格中.这花了我一些时间来弄明白(我对AngularJS来说相当新,这对我来说是ui-grid的第一个应用程序).希望这对某人有帮助,即使它没有解决这个问题.