Erez
Erez

Reputation: 594

angularJs loop not working in controller

I cant use loops on my controller and no error this is my code

'use strict';

var chartApp = angular.module('chartsApp');

chartApp.controller('chartController', ['$scope', 'ChartingService', function($scope, ChartingService){

var charts = ChartingService.query();
console.log(charts);

for (var i = 0; i < charts.length; i++) {
    console.log(charts[i]);
};


console.log('after');

}]);

and this is the cose to get the data from api

var app = angular.module('chartsApp');

app.factory('ChartingService', function($resource) {
return $resource('/api/sales', {
    id: '@id'
}, {
    add: {method: 'POST'}

});
});

i dont get any error but the loop arnt working when i console log the var charts its printing on the console window all the data

thats what i get in the console

http://prntscr.com/9lkb5y

any help?

Upvotes: 1

Views: 863

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The documentation of $resource says:

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.

(emphasis mine)

And then follows an example which uses a callback function in order to get the first element of the array returned by query(), once it's available.

Upvotes: 3

Related Questions