Susy11
Susy11

Reputation: 260

AngularJS $rootScope not working as expected

I searched for my problem but could not find a solution. I want to be able to pass a variable initialized from an http.get to an http.post in angular. The problem is that my retur array is always empty. Here you can find my plnkr code with comments on what i want to achieve. Any ideea on how to make this work would be apreciated.

Upvotes: 0

Views: 829

Answers (1)

Alexandre TRINDADE
Alexandre TRINDADE

Reputation: 947

In javascript, the calls are asynchronous. You must to call $http.post inside $http.get response:

$http.get('someurl').success(function (data) {
    var data_array = JSON.parse(data);
    var tempArray = new Array();
    tempArray['hi'] = 'hi';
    $rootScope.post_array = tempArray; 

    console.log($rootScope.post_array);
    $http.post('some-other-url',$rootScope.post_array).success(function(data){ 
        console.log(data);
    })    
});

Upvotes: 1

Related Questions