0

I don't understand how to wait for the resource call to finish before assigning the data.properties to $scope in my controller.

here is my confusion:

i have a resource call that returns this:

[
{
    "PreAlertInventory": "5.000000",
    "SharesInInventory": "3.000000",
    "TotalSharesSold": "2.000000",
    "TotalMoneySharesSold": "18.000000",
    "TotalSharesBought": "0.000000",
    "TotalShareCost": "0.000000",
    "EstimatedLosses": "0.000000"
}
]

here is the applicable code from the controller:

$scope.alertSwap = function () {

  var data = Data.query(); // gets the data. works as expected

  //$scope.test = data; //works as expected. can access data and properties in template
  $scope.test = data.TotalMoneySharesSold; //does not work. is empty???
}

i can access the array and its properties from the template BUT not from inside the controller.

How do I access the child elements from inside the controller that called the resource?

http://plnkr.co/edit/JnLqq4v7lKlYOHg85Jma?p=preview

answer: thanks to all who helped:

I needed to assign a callback to the resource call, and assign the elements from within the function:

$scope.data = Data.query(myParams, function(data) {
  $scope.test = data[0].TotalSharesBought;
});
7
  • please post Fiddle or Plunker to help us to detect the issue. Thanks Commented Sep 29, 2013 at 7:09
  • do you get json as async? Commented Sep 29, 2013 at 7:09
  • I assume Your data request is async, you need to put a callback and when it finished assign its value to '$scope.testTwo' Commented Sep 29, 2013 at 7:18
  • 1
    Your data would appear to be returned as an array, try $scope.testTwo = data[0].TotalSharesBought Commented Sep 29, 2013 at 7:24
  • @steveukx didn't notice that its list Commented Sep 29, 2013 at 7:25

2 Answers 2

1

Your data would appear to be returned as an array, try $scope.testTwo = data[0].TotalSharesBought.

I've updated your Plunkr accordingly.

Sign up to request clarification or add additional context in comments.

3 Comments

this is my actual call. how do i incorporate your answer with this? var data = Data.query({TradeID:trade.TradeID, AlertID: alertID});
Call Data.query(myJson, callbackFn) where myJson is the JSON your are posting and callbackFn is the function to call with the response data when it's available. The plunkr currently doesn't have a json parameter in the query, but does call back a response function.
Thank you for your help i was seriously over complicating this. answer $scope.data = Data.query(myParams, function(data) { $scope.test = data[0].TotalSharesBought; });
1

As said above it is likely you are fetching those data async. I updated your code to support promise (which makes your code async and may not fit with the rest of the application by the way).

$timeout simulates an async call. In your factory service:

var deferred = $q.defer();
$timeout(function() {
    return deferred.resolve(data);
}, 100); // set it to zero to return immediately
return deferred.promise;

In your controller call you factory factory method query() and hook the response with then, which is the promise part of the snippet:

Data.query().then(function(response) {
  $scope.test = response[0].TotalMoneySharesSold;
});

http://plnkr.co/edit/SGGCVS?p=preview

4 Comments

He loads all his json data to $scope.data but can't parse it. Ther is no async problem
how do i get my factory to return the "response"? this is the factory: .factory('AlertLoss', function($resource){ return $resource('localhost/shfofx/PHP/Rest/alertLossDetails.php', {}, { query: {method:'GET', params:{}, isArray:true} }); });
i got this code working but it still produces the exact same issue as in the post. the entire array loads but I cannot assign individual elements from inside the controller
I might not have fully understood your question, but you didn't mention clearly you want to assign a value inside your controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.