2

My Code :

HTML

<body ng-app="loginApp" ng-controller="loginCtrl" > <div class="loginWrap" > <input type="text" placeholder="Username " ng-model="user" /> <input type="text" placeholder="Password " ng-model="pass" /> <input type="button" value="Login" ng-click="checkLogin()" /> <span ng-bind="resultLog"></span> </div> </body>

Angular Code :::

 var appLog = angular.module('loginApp',[])
 .controller('loginCtrl',function($scope,$http){

 $scope.checkLogin = function(){

  if($scope.user.length > 0 && $scope.name.length > 0  )
  {
      $http.post('php/checkLogin.php',{'n':$scope.user, 'p':$scope.pass})
         .success(function(data)
         {
            if(data)
            {
                $scope.resultLog = "Valid"
            }
             else
            {
                $scope.resultLog = "InValid"
            }
         })
         .error(function()
         {
           $scope.resultLog="Error"
         })
  }
  else
  {
    $scope.resultLog = "Fill All Fields";
  } // end of validation


 } })

I am learning angular JS.. and execute some..

Problem :: $scope.resultLog Not working

Scope Obejct not working inside the function.. it is declare in LoginCtrl and i m using inside the function, declare inside the LoginCtrl

Thanks in advance

2
  • check my answer .....its working only thing you have to initialize user and name other wise it will be undefined so you cannot check the length of undefined Commented Apr 3, 2014 at 8:46
  • @Mik378 ... may be its $scope.pass.. i wrote wrong Commented Apr 3, 2014 at 8:59

2 Answers 2

2

In your controller, you use $scope.name, what does it point to? since you have ng-model="user", not ng-model="name".

Your input doesn't point to user.name directly but to user. You want to change it like that:

<input type="text" placeholder="Username " ng-model="user.name" />

and in your controller:

$scope.checkLogin = function(){

  $scope.user = {};    

  if($scope.user.name.length > 0 && $scope.user.name.length > 0  )

  //......

}
Sign up to request clarification or add additional context in comments.

1 Comment

@Mik378 +1 for you answer
1

The only thing you have to initialize user and name other wise it will be undefined so you cannot check the length of undefined

Try this one

Working Demo

var appLog = angular.module('loginApp',[]);

appLog.controller('loginCtrl',function($scope,$http){
$scope.user="";
$scope.pass="";
$scope.checkLogin = function(){

if($scope.user.length > 0 && $scope.pass.length > 0  )
{
      $http.post('php/checkLogin.php',{'n':$scope.user, 'p':$scope.pass})
         .success(function(data)
         {
            if(data)
            {
                $scope.resultLog = "Valid"
            }
             else
            {
                $scope.resultLog = "InValid"
            }
         })
         .error(function()
         {
           $scope.resultLog="Error"
         })
}
else
{
  $scope.resultLog = "Fill All Fields";
} // end of validation

} });

2 Comments

What is the point to have $scope.user and $scope.name separately? Makes no sense to me
But user and pass are strictly related, it composes credentials. I'd prefer have it in the same scope variable like $scope.credentials.name, $scope.credentials.pass. Indeed you are in a "Login" controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.