简体   繁体   中英

how to send values from view controls to angularjs controller..?

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>

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
}

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)" />
use this 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM