1

i am beginner in angular, so sorry if my question is dummy i have some code like this

var data = [];
$scope.dataObj = {};
resourceTag.getTag({tagName: 'قیچی'}, function(json){

    angular.forEach(json, function(value, key){
        data.push({
            id: value['id'],
            text: value['title']
        });
    });

    $scope.dataObj = data;
});

console.log($scope.dataObj);

but in console i get {} (null) but when try this :

var data = [];
$scope.dataObj = {};
resourceTag.getTag({tagName: 'قیچی'}, function(json){

    angular.forEach(json, function(value, key){
        data.push({
            id: value['id'],
            text: value['title']
        });
    });

    $scope.dataObj = data;
    console.log($scope.dataObj);
});

this is working and console show my data , i guess this issue Related to $digest and $apply or maybe i Commit big mistake

this issue have any best practice (pass scope to out of function !)

Alireza Davoodi
  • 749
  • 7
  • 20
  • getTag is asynchronous. http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Benjamin Gruenbaum Jun 30 '13 at 11:21
  • this has nothing to do with angular in particular, it's the way callback functions work. i.e. what is inside `function(value, key){` might very well be executed after the `console.log` at the end. – mb21 Jun 30 '13 at 12:02

0 Answers0