Sud Sud - 15 days ago 6
AngularJS Question

Execution is not going inside a function in a angularjs controller

I have a problem in calling a function

checkLogin
of a controller.
I am using the value returned by
checkLogin
to hide/show with
ng-show
. When i check by debugging the execution is happening until function call and later on it is just skipping to execute that function so i am not getting any thing in return please see the following code and let me know if anything is wrong out here..

The function i am calling is checkLogin() from the LoginController please observe that function

Angular Module

'use strict';

var mainApp=angular.module('MainModule',['ngRoute']);
mainApp.constant('encrypt','');
mainApp.config(function($routeProvider){
$routeProvider.when("/",{
templateUrl : "index.html",
controller : 'LoginController'
})
.when("/loginpage",{
templateUrl : "ataclient/login/loginpage.html",
controller : 'LoginController'})
.when("/registration",{
templateUrl: "ataclient/register/register.html",
controller: 'RegistrationController'
});
});


Angular Controller

'use strict';

function LoginController(encrypt,$http,$scope,$location){

$scope.userName = "";
$scope.password ="";
$scope.loginstatus=true;
$scope.data="";
$scope.loginSubmit = function(){
alert("inside login submit");
if($scope.userName && $scope.password){
$http({
url:'http://localhost:8080/com/rest/user/login',
method:"POST",
headers:{
'Content-Type':'application/json'
},
data : {
"userName" : $scope.userName,
"passwd" : $scope.password
}
}).then(function(response){$scope.data = response;});
if($scope.data.data.encryptedkey != null ){
encrypt = $scope.data.data.encryptedkey ;
$scope.loginstatus = false;
console.log($scope.loginstatus);
alert(encrypt);
}
}
};
$scope.checkLogin = function(){
alert("inside checkLogin");
if(encrypt != null){
return false;
}
else
{
return true;
}
};

this.login=function(){
alert("for u in sss");
$http({
url:'http://localhost:8080/com/browsers',
method:"GET",
headers:{
'Content-Type':'application/json',
'SessionId': 1478785782179
}
}).
then(function(Response){$scope.data=Response;});
};
}

LoginController.$inject = ['encrypt','$http' ,'$scope','$location'];

mainApp.controller('LoginController', LoginController);


and the html piece of code using to call is
html piece

<form ng-submit="loginSubmit()" ng-controller = "LoginController">

<div class="col-md-12 col-lg-12 col-xl-12 text-center" id="logintitle">Login Here</div>

<div class="col-md-offset-2 col-md-8 col-md-offset-2 col-lg-offset-2 col-lg-8 col-lg-offset-2 col-xl-offset-2 col-xl-8 col-xl-offset-2 form-group">
<label for="UserName"> User Name</label>
<input type="UserName" ng-model="userName" placeholder="Enter UserName" class="form-control" id="username" aria-describedby="UserNamehelp">
</div>
<div class="col-md-offset-2 col-md-8 col-md-offset-2 col-lg-offset-2 col-lg-8 col-lg-offset-2 col-xl-offset-2 col-xl-8 col-xl-offset-2 form-group">
<label for="UserName"> User Name</label>
<input type="Password" ng-model="password" placeholder="Enter Password" class="form-control" id="password" aria-describedby="PassWordHelp">
</div>
<div class="col-md-offset-2 col-md-4 col-lg-offset-2 col-lg-4 col-xl-offset-2 col-xl-4">
<button ng-show ="checkLogin()" type="submit" class="btn btn-primary" id="submit">Submit {{data.data.encryptedkey}}</button>
</div>
<div class="col-md-4 col-md-offset-2 col-lg-4 col-lg-offset-2 col-xl-4 col-xl-offset-2 ">
<button type="submit" class="btn btn-primary" id="forgot">forgot</button>
</div>
</form>

Answer

You are assigning a value to the constant provider encrypt

mainApp.constant('encrypt','');
...
if($scope.data.data.encryptedkey != null ){
  encrypt = $scope.data.data.encryptedkey;
...

The name of the provider implies it should not be changed, I would suggest using the value provider instead.

Your real problem, as Duncan has mentioned, is that your if block is outside of your http response block

...
}).then(function(response){$scope.data = response;});
  if($scope.data.data.encryptedkey != null ){  //<--- problem!
     encrypt = $scope.data.data.encryptedkey;
...

change this to the following and you'll be a step close to success~~

...
}).then(function(response){
  $scope.data = response;});
  if($scope.data.data.encryptedkey != null ){
     encrypt = $scope.data.data.encryptedkey;
     $scope.loginstatus = false;
     console.log($scope.loginstatus);
     alert(encrypt);
  }
});
...
Comments