我尝试使用和ngResource $资源调用来填充ng-grid,但似乎在ng-grid已经渲染之后,ajax调用总是来不及。 网格呈现标题但不填充数据。 这是代码片段和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>
答案 0 :(得分:1)
您需要返回填充网格的承诺,将svc.query()更改为
$scope.myData={};
SERVICE.query()
.$promise.then(function(data) {
$scope.myData=data;
}, function() {
//TODO:Handle Error
});
答案 1 :(得分:0)
请像这样编辑标记html。
<body ng-app="myModule">
<div ng-controller="GridController">
<div class="gridStyle" ng-grid="gridOptions" ></div>
</div>