ng-grid doesn't populate with angular $resource

百般思念 提交于 2019-12-24 12:25:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!