I am dealing with a situation where I have a resource returned from the resolve parameter of my route:
resolve: {
item: function(Orders, $route) {
return Orders.get({ id: $route.current.params.id });
}
}
The structure of the 'item' object is as follows:
var item = {
name: 'John',
products: [{
id: 123
}, {
id: 234
}]
}
In my controller, I need to watch the products array within this 'item' object:
app.controller('ViewOrderCtrl', function($scope, item) {
$scope.item = item;
$scope.$watch('item.products', function(n, o) {
// This sometimes throws an error due to 'undefined' not being an object
$scope.item.products.forEach(function(product) { });
}, true);
});
I believe the issue arises because the resource is not fully loaded at the beginning, resulting in undefined values. For instance, accessing $scope.item.name
will also return as undefined.
Is there any way to handle this situation without explicitly checking for the presence of 'products' in my watch? I am aware that I can do the following:
if($scope.item.products) {
$scope.item.products.forEach(function(product) { });
}
However, I am curious if there is a specific setting or technique to address this delay issue more effectively.