NG-网格不具有角$资源填充(ng-grid doesn't populate with a

2019-10-20 04:38发布

我试图填充一个NG-格,使用和ngResource $资源调用,但似乎Ajax调用总是为时已晚,NG-电网已经被渲染后。 网格渲染的标题,但没有填充数据。 下面的代码和一个片段的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>

Answer 1:

请编辑标记HTML这样的。

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



Answer 2:

你需要返回一个承诺网格填充,更改svc.query()来

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

            }, function() {
                //TODO:Handle Error
            });


文章来源: ng-grid doesn't populate with angular $resource