Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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

share|improve this question
    
What is $scope.name? –  Mik378 Apr 3 '14 at 8:38
    
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 –  Nidhish Krishnan Apr 3 '14 at 8:46
    
@Mik378 ... may be its $scope.pass.. i wrote wrong –  Ranjit Apr 3 '14 at 8:59

2 Answers 2

up vote 0 down vote accepted

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

} });
share|improve this answer
    
What is the point to have $scope.user and $scope.name separately? Makes no sense to me –  Mik378 Apr 3 '14 at 8:47
    
it can be changed to $scope.user and $scope.pass.....i've updated my answer –  Nidhish Krishnan Apr 3 '14 at 8:50
    
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. –  Mik378 Apr 3 '14 at 8:52
    
thnks a alot .. –  Ranjit Apr 3 '14 at 8:56
    
@Mik378 you are right.....but its not compulsary right.. –  Nidhish Krishnan Apr 3 '14 at 8:57

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  )

  //......

}
share|improve this answer
    
ok... i got it... thanks –  Ranjit Apr 3 '14 at 8:56
    
@Mik378 +1 for you answer –  Nidhish Krishnan Apr 3 '14 at 9:37
    
@NidhishKrishnan Thanks ;) –  Mik378 Apr 3 '14 at 9:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.