Answer the question
In order to leave comments, you need to log in
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
count:
$resource('api/companies/count', {}, {
'query': {method:'GET',isArray:false},
}),
Answer the question
In order to leave comments, you need to log in
Maybe because the request is asynchronous, and at the time of the call (where undefined appears) - $scope.count is really undefined ?
Because a function success
is 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 questionAsk a Question
731 491 924 answers to any question