1

我正在尝试填充 ng-grid,使用和 ngResource $resource 调用,但在 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>
4

2 回答 2

1

您需要返回一个要填​​充网格的承诺,将 svc.query() 更改为

$scope.myData={};
 SERVICE.query()
            .$promise.then(function(data) {
          $scope.myData=data;

            }, function() {
                //TODO:Handle Error
            });
于 2014-05-28T19:16:47.177 回答
0

请像这样编辑标记html。

<body  ng-app="myModule">
<div ng-controller="GridController">
    <div class="gridStyle" ng-grid="gridOptions" ></div>
</div>

于 2014-05-29T12:06:33.327 回答