问题
I'm trying to populate an ng-grid, using and ngResource $resource call, but it seems that the ajax call is always too late, after the ng-grid has already been rendered. The grid renders the headers but is not populated with data. Here's a snippet of the code and a jsfiddle
angular.module('api', [ 'ngResource' ]).factory("SERVICE", function($resource) {
return $resource('/api/...', {}, {
query : {
method : 'GET',
isArray : true,
transformResponse : function(data, headers) {
var responsedata = [], data = angular.fromJson(data);
angular.forEach(data.el, function(item, idx) {
//do something to data and add it to responsedata
responsedata.push({"a": item.a});
});
return responsedata;
}
}
});
});
var app = angular.module('myModule', [ 'ngResource', 'api', 'ngGrid' ]);
app.controller('GridController', [ '$scope', 'SERVICE', function($scope, svc) {
$scope.myData = svc.query();
$scope.gridOptions = {
data : myData,
columnDefs : [{
field : "a",
displayName : "a"
}]
};
} ]);
<!-- the html-->
<div class="gridStyle" ng-grid="gridOptions" ng-controller="GridController"></div>
回答1:
Please edit the markup html like this.
<body ng-app="myModule">
<div ng-controller="GridController">
<div class="gridStyle" ng-grid="gridOptions" ></div>
</div>
回答2:
You need to return a promise for the grid to populate, change svc.query() to
$scope.myData={};
SERVICE.query()
.$promise.then(function(data) {
$scope.myData=data;
}, function() {
//TODO:Handle Error
});
来源:https://stackoverflow.com/questions/23919962/ng-grid-doesnt-populate-with-angular-resource