angular验证的小白问题

yaotomo yaotomo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
需求很简单,在页面点击登录按钮,如果用户名或密码为空,就alert提示用户
前几天学过form验证,现在我没有使用表单,仅仅是两个文本框使用ajax验证
用户名和密码的ng-model分别是data.username和data.password
但是在js代码中 console.log($scope.data.username,$scope.data.password)得到的值为undefined
即使用户名和密码框都输入了值也是undefinded
而且同一次点击,console.log得不到值,下面post提交的$scope.data.username和$scope.data.password却有值

前台login.aspx

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>系统登录</title>
<link href="styles/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="styles/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body { margin-top:50px; }
</style>
</head>
<body>
<div ng-app="loginApp">
<div class="container" ng-controller="loginController">

<div class="form-horizontal login-box">
<div class="form-group">
<div class="col-sm-2"><label class="control-label" for="txtUsername">用户名</label></div>
<div class="col-sm-10">
<input type="text" id="txtUsername" class="form-control" placeholder="用户名" ng-model="data.username">
</div>
</div>
<div class="form-group">
<div class="col-sm-2"><label class="control-label" for="txtUsername">密码</label></div>
<div class="col-sm-10">
<input type="password" id="txtPassword" class="form-control" placeholder="密码" ng-model="data.password">
</div>
</div>
<div class="form-group">
<div class="col-sm-12 text-right">
<button type="button" class="btn btn-primary" ng-click="login()">登录</button>
</div>
</div>
</div>
</div>
</div>
<script src="scripts/angular.min.js" type="text/javascript"></script>
<script src="scripts/login.js" type="text/javascript"></script>
</body>
</html>


JS代码login.js

var app = angular.module("loginApp", [])
.controller("loginController", ["$scope", "$http", function ($scope, $http) {
$scope.login = function () {
$scope.data = {};
console.log($scope.data.username,$scope.data.password);
// if($scope.data.username == undefined || $scope.data.password == undefined){
// alert("用户名和密码不能为空!");
// return false;
// }
$http({
method : "POST",
url : "../handlers/LoginHandler.ashx",
params : { username : $scope.data.username , password : $scope.data.password }
}).then(function successCallback(response){
if(response.data == 0){
alert("用户名或密码错误!");
}else{
location.href = "index.aspx";
}
},function errorCallback(){

});
}
} ]);
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
yaotomo
.NET社区 62,275 成员 668,968 内容