0

I can not access an array in the AngularJS controller but it works in the view.

In the controller: .results returns undefined

function TwitterCtrl($scope, $resource){

  $scope.twitter = $resource('http://search.twitter.com/:action',
      {action:'search.json', q:'angularjs', callback:'JSON_CALLBACK'},
      {get:{method:'JSONP', params: {rpp: 4}}});

    $scope.twitterResult = $scope.twitter.get({q:"example"});

    //returns the resource object
    console.log($scope.twitterResult)

    //returns undefined
    console.log($scope.twitterResult.results);
}

In the view: .results returns an array of tweets

//This returns an array of tweets
{{$scope.twitterResult.results}}
3
  • See also stackoverflow.com/questions/14373661/angularjs-data-assignment Commented Mar 18, 2013 at 13:59
  • Thanks mark. That post describes what I've been trying to achieve. I put all of my code in the success callback in order to use the results array. I wish I could assign the array to a variable and access it outside of that function. Commented Mar 18, 2013 at 16:02
  • You can do that -- e.g., $scope.twitterResult = $scope.twitter.get(...)` -- as long as you also add a $watch('twitterResult', function(newValue) { ... }) to detect when it gets asynchronously updated. Commented Mar 18, 2013 at 16:10

1 Answer 1

5

$resource calls are asynchronous, but $resource service returns a blank object for resource.get calls immediately on invocation (or empty array on resource.query calls). Then, only after the promise gets resolved (server returns response), the $resource service assigns the actual results to the $scope.twitterResult variable.

That's why $scope.twitterResult is blank on immediate access (console.log), but (seemingly) 'works' in your view.

Your view expression {{$scope.twitterResult.results}} is also undefined at first, but Angular's $parse service (responsible for parsing view expressions) does not output undefined because it's designed not to. As soon as server response is received, the view expression is updated, and twitterResult.results are displayed.

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

2 Comments

$scope.twitterResult is not blank when using console.log in the controller. It returns the resource object. I can see the results array that I want to access when it's logged. However, I get undefined when I use $scope.twitterResult.results in the controller.
@Dru, by the time you manually click the twitterResult array in the console, the results have been populated (asynchronously). This is the way Chrome works. If you instead log console.log(JSON.stringify($scope.twitterResult)) you will see that results are not populated at the time this statement actually executes.

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.