Randheer Singh Chouhan Randheer Singh Chouhan - 3 months ago 58
AngularJS Question

How to wait till the response comes from the $http request and assign value in $scope variable in angularjs?

this is the situation :

$scope.signupNow = function () {
retrieveData();
console.log($scope.userid);
}

function retrieveData(){
// ** some http request **
$scope.userid = id;
}


now http request take some time to execute and there is no value in "$scope.userid" because it's just print after calling the function.

Can you please suggest that How to wait till the response comes????

Answer

ok if u want to wait until http response then u can use a callback function

$scope.signupNow = function () {

    retrieveData(function(){

        console.log($scope.userid);
    });   
}

function retrieveData(callback){
    // ** some http request **
    $scope.userid = id; 
    callback();
}
Comments