In my current project, I have a list of JSON objects called $scope.phones and a directory filled with JSON files containing extra data about each phone. My goal is to go through these files and extract additional information to enhance the details in my phone list:
phonecatControllers.controller('PhoneListCtrl', ['$scope', 'Phone',
function($scope, Phone) {
$scope.phones = Phone.query();
var myTimer = window.setTimeout(function() {
for (var i = 0; i < $scope.phones.length; i++){
var weight = 0;
Phone.get({phoneId: $scope.phones[i].id}, function(phone) {
weight = phone.sizeAndWeight.weight;
$scope.phones[i].weight = weight;
});
};
} , 1000 );
$scope.orderProp = 'age';
}]);
The setTimeout function is used to ensure that the code only runs after $scope.phones are populated. Although it might be considered a bit of a workaround, it's currently functioning fine. However, an issue arises with the line:
$scope.phones[i].weight = weight;
This error "cannot set property of undefined" occurs because trying to access this outside the .get method works without errors, but the updated value of weight remains confined within the .get method.