0

I have a piece of code that I only seem to be able to access inside of then. I would like to get access to it outside so that I can use it in other places.

$scope.model = {'first_name':'','last_name':'','email':'',};
djangoAuth.profile().then(function(data){
    $scope.model = data;
    console.log($scope.model); //this prints the data
});

console.log($scope.model); //this prints empty data 

The profile code is like this:

    'profile': function(){
        return this.request({
            'method': "GET",
            'url': "/user/"
        });
    }
birdy
  • 9,286
  • 24
  • 107
  • 171
  • If that was possible, then `djangoAuth.profile()` could simply return the result directly and you wouldn't have to provide a callback. The fact that you have to provide a callback tells you that the process is most likely **asynchronous**. – Felix Kling Mar 19 '15 at 15:28
  • My intent was not to ask why it not working but how to do it in AngularJS. I understand what async means. I found my answer in a related question http://stackoverflow.com/questions/17684153/angular-js-pass-data-from-async-service-to-scope – birdy Mar 19 '15 at 15:41
  • Next time, ask exactly what you want to know. "How to access the data outside of then" is very far from "how to pass data from async service to scope". – Felix Kling Mar 19 '15 at 15:43

1 Answers1

1

That's because then is async, even if the promise has already resolved. Angular waits until a timer-tick before it calls promise callbacks.

You can do a scope-digest to force it to evaluate immediately, but that has performance implications.

bvaughn
  • 13,300
  • 45
  • 46