0

In my Angularjs MVC application i write code for lo-gin which accept values from two text boxes as user name & password my Angularjs controller code is as which works fine but problem with view

     $scope.Login = function Login(U_Name, U_PWD) {
      //Defining the $http service for  login the admin user
      $http({
          method: 'POST',
          url: '/Admin/IsAuthenticate',
          data: { User_Name: U_Name, 

              User_PWD: U_PWD }
      }).success(function (result) {

          if (result == true) {
              alert('user is valid');
          }
          else {
              alert('unauthorised access!');
          }
      })
          .error(function (error) {
              //Showing error message 
              $scope.status = 'Unable to connect' + error.message;
          });
  }

this code is executing on ng-click event but doesn't accept values from input boxes my view code is as:

  <div ng-app="angApp">
     <div ng-controller="AdminCtrl">
        <div class="admin-login">
            <h2>using angularjs</h2>
            <input type="text" id="txtUserAng" placeholder="User Name" ng-model="U_Name" />
            <input type="password" id="txtPWDAng" placeholder="Password" ng-model="U_PWD" />
            <input type="button" id="login" value="login" ng-click="Login()" />
        </div>
      </div>
   </div>

3 Answers 3

1

Your problem is that you are not passing U_Name, U_PWD variable to Login function. You need to pass input values to your function.

There are 2 ways of achieving it.

First, Pass the model directly to function like

 <input type="button" id="login" value="login" ng-click="Login(U_Name, U_PWD)" />

Second, Use $scope.U_Name and $scope.U_PWD

$scope.Login = function() {      
    var U_Name = $scope.U_Name, 
        U_PWD = $scope.U_PWD
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Satpal <input type="button" id="login" value="login" ng-click="Login(U_Name, U_PWD)" /> first approach work fine but second doesn't work
1

In controller get this

this example without set login parametr

   $scope.Login = function Login() {
     var U_Name = $scope.U_Name;
     var U_PWD = $scope.U_PWD;
    console.log($scope.U_Name);
    console.log($scope.U_PWD);
    }

And you can set login function parrameters that model

<input type="button" id="login" value="login" ng-click="Login(U_Name, U_PWD)" />

1 Comment

Thanks @Harutyun <input type="button" id="login" value="login" ng-click="Login(U_Name, U_PWD)" /> this work fine but first approach doesn't work
1
use this 

data: 
{ 
User_Name: $scope.U_Name, 
User_PWD: $scope.U_PWD 
} 

Comments

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.