A
A
Artur Bekerov2014-01-26 21:05:27
API
Artur Bekerov, 2014-01-26 21:05:27

Angular, why is $scope not visible outside of the request?

Why is $scope not visible outside of the request?
How can I reach him?

Companies.count.query({}, function success(data) { 
              $scope.count = data; 
              console.log($scope.count.c)         //print 4415
          })
      console.log($scope.count.c)         //undefined

Part of the service:
count:
                $resource('api/companies/count', {}, {
                    'query':  {method:'GET',isArray:false},
                }),

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Damir Makhmutov, 2014-01-26
@doodoo

Maybe because the request is asynchronous, and at the time of the call (where undefined appears) - $scope.count is really undefined ?

G
goodnickoff, 2014-01-27
@goodnickoff

Because a function successis a callback that is called after the request has completed. In your second example, the variable is аassigned an empty object (or an array, if it is in the options isArray) which is filled with data after the request has completed and the data is visible in the console, because by the time you view them, the request has already completed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question