1

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>

2 Answers 2

1

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
            });
Sign up to request clarification or add additional context in comments.

3 Comments

Changed the code to svc.query().$promise.then(function(data) { $scope.myData = data; }, function() {alert('failed');}); But I get the same behavior, still no data populated.
Hmm, can you create a plunker? With your data mocked out instead of a true service call?
the grid has the data in but i thing the way the odata is represented is the problem. The nggrid scope is having data but is not able to acces it.
0

Please edit the markup html like this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.