I am working on a project where there is a page having a input field named balance and the div containing this field is balanceDiv.for admin this field will not show.so i use ng-show to hide/show that div,wrote function in angular controller (one for collecting loginId and another one for hide or show div according to that login id).the condition for show or hide that div is--- if admin found(id 1) div will hide otherwise div will show.To do that i notice that when calling ajax-- execution switching to second function.In my angular controller--
init();
function init()
{
setLoginId();
showHide();
initilize();
}
you can see that first it will execute setLoginId then it will go for showHide (As i expected).The two functions---
function setLoginId()
{
var apiRoute = baseUrl + '/api/AmountDists/GetLoginId/';
var result = CrudService.getAll(apiRoute);
result.then(function (response) {
debugger
$scope.loginId = response.data;
},
function (error) {
console.log("Error: " + error);
});
}
function showHide() {
if ($scope.loginId == 1) {
$scope.balanceDiv = false;
}
else {
$scope.balanceDiv = true;
}
}
what i have noticed is that when it is executing the ajax call in setLoginId method after that without getting the response (or completting the method) it goes for showHide method.After completing showHide method it again return in setLoginId method.Then it execute the rest of the method instruction.Result is that i am getting undefined value for $scope.loginId in showHide method.
I want to know why this happens? why execution switching to another method when ajax calling?