如何将视图控件的值发送到angularjs控制器..?
在我Angularjs MVC应用程序,我写的LO-杜松子酒从两个文本框的用户名&密码 我Angularjs控制器代码是接受值码作为工作正常,但有鉴于如何将视图控件的值发送到angularjs控制器..?
$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;
});
}
这个代码是问题上执行NG单击事件,但不接受输入框中的值 我看来代码如下:
<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>
你的问题是,你不及格U_Name, U_PWD
变量Login
FUNC灰。您需要将input
值传递给您的函数。
有两种实现方法。
首先,直接传递模型的功能类似
<input type="button" id="login" value="login" ng-click="Login(U_Name, U_PWD)" />
二,使用$scope.U_Name
和$scope.U_PWD
$scope.Login = function() {
var U_Name = $scope.U_Name,
U_PWD = $scope.U_PWD
}
在控制器得到这个
这个例子中没有设置login
parame TR
$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);
}
并且可以设置login
功能parrameters该模型
<input type="button" id="login" value="login" ng-click="Login(U_Name, U_PWD)" />
感谢@Harutyun 这个工作很好,但第一种方法不起作用 – 2014-10-31 07:13:10
现在都在工作 谢谢 – 2014-10-31 07:17:06
use this
data:
{
User_Name: $scope.U_Name,
User_PWD: $scope.U_PWD
}
由于@Satpal 第一种方法工作正常但第二种不起作用 – 2014-10-31 07:13:57