I'm developing an AngularJs application that fetches via Angular Resource.
I receive the array of data just fine, and view the records inside a table view via ng-repeat
and everything works great.
The problem is that, when I process my array of data using underscore.js
inside my service, ng-repeat
gets an empty array. Despite that if I log the same scope variable after the the processing, I get the result as expected.
My Angular Service code:
app.factory("DataProcessor", function() {
return {
ToAssocArray: function(input) {
retData = [];
_.each(input.data, function(elmnt, i, list) {
retData[elmnt.id] = elmnt;
});
return retData;
}
};
});
Code snippet from my controller:
app.controller("main_ctrl", function($scope, $timeout, Data, DataProcessor) {
...
$scope.serverData = [];
$scope.myData = [];
$scope.myDataProcessed = [];
$scope.serverData = Data.query({});
$scope.serverData.$promise.then(
function onSuccess(result) {
$scope.myData = result; // here I receive the data just fine.
$scope.myDataProcessed = DataProcessor.ToAssocArray(result); // my data processing
console.log($scope.myDataProcessed); // processed data gets logged flawlessly
},
);
And inside my html when I type:
{{myDataProcessed}}
I get:
[]
[]
as a result. – Muhammad Reda Dec 11 '13 at 14:16_each
and inspect variables – charlietfl Dec 11 '13 at 14:41