I am experiencing an issue with calling the checkLogin
function of a controller. The value returned by checkLogin
is used to show/hide elements with ng-show
. However, when debugging, I noticed that the execution stops after the function call and does not proceed to execute the function itself. This results in me not receiving any return value. Please review the following code snippet and let me know if you spot any errors.
The function I am trying to call is checkLogin() from the LoginController. Please take a look at 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);
Below is the HTML code snippet where the mentioned functions are being called: 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="Password"> Password</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>