I'm having trouble understanding how to ensure the resource call completes before assigning the data.properties to $scope in my controller.
My confusion lies here:
The resource call returns the following response:
[
{
"PreAlertInventory": "5.000000",
"SharesInInventory": "3.000000",
"TotalSharesSold": "2.000000",
"TotalMoneySharesSold": "18.000000",
"TotalSharesBought": "0.000000",
"TotalShareCost": "0.000000",
"EstimatedLosses": "0.000000"
}
]
Here is the relevant code from the controller:
$scope.alertSwap = function () {
var data = Data.query(); // retrieves the data, functioning as expected
//$scope.test = data; // works as expected, able to access data and properties in template
$scope.test = data.TotalMoneySharesSold; // not functioning, remains empty???
}
I can access the array and its properties from the template, but not from within the controller itself.
How can I access the child elements from inside the controller that initiated the resource call?
http://plnkr.co/edit/JnLqq4v7lKlYOHg85Jma?p=preview
Answer: Much appreciation for everyone's assistance:
I realized I need to assign a callback to the resource call, and retrieve the elements within that function:
$scope.data = Data.query(myParams, function(data) {
$scope.test = data[0].TotalSharesBought;
});