Maria Jane Maria Jane - 4 months ago 23
AngularJS Question

Angularjs promised using $http

$http({
url: '/checkUsername',
data:{name:$scope.username},
method: "POST"
})
.then(function(response) {
$scope.gameStart = true;
})


For some reason $scope.gameStart cannot be within then(), it has to be outside. How do I use $scope.gameStart after my ajax call finished, without putting my code within it?

Answer

Some function instead bunch of code

$http({
    url: '/checkUsername',
    data:{name:$scope.username},
    method: "POST"
})
.then(bunchOfCode)

function bunchOfCode(response) {
    console.log('response', response); // still have access to it
    $scope.gameStart = true;
}