angularjs中$http、$location、$watch及双向数据绑定学习实现简单登陆验证

简介: <p>使用$http、$location、$watch及双向数据绑定等实现简单的用户登陆验证,记录备忘:</p> <p>1、$http模拟从后台获取json格式的数据;</p> <p>2、$watch实时监控数据变化;</p> <p>3、$location.path实现页面跳转;</p> <p>4、$scope实现数据绑定,包括input内容及文字样式等</p> <p><br><

使用$http、$location、$watch及双向数据绑定等实现简单的用户登陆验证,记录备忘:

1、$http模拟从后台获取json格式的数据;

2、$watch实时监控数据变化;

3、$location.path实现页面跳转;

4、$scope实现数据绑定,包括input内容及文字样式等


app.js代码

var app=angular.module('contactsApp',['ui.router']);
app.config(function($stateProvider,$urlRouterProvider){
    $urlRouterProvider.otherwise('/login');
		$stateProvider.state('login', {
			url: "/login",
			views: {
				'view': {
					templateUrl: 'views/login.html',
					controller: 'loginCtr'
				}
			}
		});
});
首页contacts.html代码:

<!DOCTYPE html>
<html ng-app="contactsApp">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="./angular-1.4.0-rc.2/angular.js"></script>
  <link rel="stylesheet" href="./css/login.css"/>
  <script src='./angular-1.4.0-rc.2/angular-ui-router.js'></script>
  <script src='./scripts/app.js'></script>
  <script src="./scripts/controllers/loginCtr.js"></script>
  <script src="./scripts/controllers/blogCtr.js"></script>
</head>
<body>
  <center>
  <!--顶部标题-->
  <div id="naDiv">
  	 My  ContactsSystem
  </div>
</br>
  </center>
  <!--使用ui-rourer控制页面之间的切换-->
  <div ui-view="view"></div>
 <center>
<!--页面底部固定内容-->
<div id="footDiv">
<hr/>
链接:
<a href="http://www.baidu.com">百度</a>
 
<a href="http://www.sina.com.cn">新浪</a>
 
<a href="http://blog.csdn.net/tuzongxun">CSDN</a>
 
<hr/>
&copy;版权所有:aaaa
<p>
	联系我:
	<a href="mailto:tuzongxun123@163.com">tuzongxun123@163.com</a>
</p>
</center>
</div>
</body>
</html>



login.html代码:

<center>
<div id="logdiv1">
	userName:<input type="text" ng-model="user.userName"/><p class="p1" id="{{isUserId}}">{{isUserName}}</p>
    </br>
    </br>
    password:<input id="logPwd" type="password" ng-model="user.userPwd"/><p class="p1" id="{{isPwdId}}">{{isPwd}}</p>
    </br>
    </br>
    <input class="bonInput" type="button" value="login" ng-click="Login(user);" ng-disabled="login">
      
    <input class="bonInput" type="button" value="regist">
</div>
</center>

loginCtr.js代码:

angular.module('contactsApp')
.controller('loginCtr', ['$scope','$http','$location', function($scope,$http,$location){
	//模拟后台用户名和密码数据
	var userNames=['aaa','bbb','ccc'];
	var userPwd='123';
	//用户名和密码初始提示文字
	$scope.isUserName='请输入用户名';
	$scope.isPwd='请输入密码';
    //是否启用登陆按钮
	var isLogin1=false;
	var isLogin2=false;
	$scope.login=true;
	//用户验证,随输入实时提醒,这里只判断用户名是否存在和空值,还可以验证输入字符的长度、格式等
	$scope.existUser=function(){
		for(var i=0;i<userNames.length;i++){
	    if($scope.user!=null&&$scope.user.userName!=''&&userNames[i]==$scope.user.userName){
           isLogin1=true;
           $scope.isUserName='用户名存在';
           //用户名存在时,通过更改id,更改提示文字的颜色为绿色
           $scope.isUserId="p2";
           break;
	    }else if($scope.user==null||$scope.user.userName==''){
           isLogin1=false;
	       $scope.isUserName='请输入用户名';
	       $scope.isUserId="p1";
	       //document.getElementById('p1').style.color='green';
	    }else{
	       isLogin1=false;
	       $scope.isUserName='用户名不存在';
	       $scope.isUserId="p1";
	       //document.getElementById('p1').style.color='green';
	    }
	    }
	}
    //legal:合法,判断密码输入是否合法,这里只判断长度,还可以正则表达式匹配,限定数字和字母
    $scope.legalPwd=function(){
        if($scope.user!=null&&$scope.user.userPwd!=null&&$scope.user.userPwd.length>=6&&$scope.user.userPwd.length<=15){
           isLogin2=true;
           //密码格式正确时,修改id,使提示文字颜色变绿
           $scope.isPwdId="p4";
           $scope.isPwd="密码格式正确";
        }else{
           isLogin2=false;
           $scope.isPwdId="p3";
           $scope.isPwd="密码格式不正确";
        }
    }

    //调用用户验证和密码验证方法,判断登陆按钮是否可用
    $scope.isLogin=function(){
    	$scope.existUser();
		$scope.legalPwd();
		if(isLogin1&&isLogin2){
		   //当用户名存在,并且密码格式正确的时候,启用登陆按钮
           $scope.login=false;
		}else{
		   $scope.login=true;
		}
    }
    //登陆跳转,传数据到后台判断用户名和密码是否匹配,模拟http请求
    $scope.Login=function(user){
    	//模拟http请求,从后台返回数据
        $http.get("./json/user.json",user).success(function(checkLogin){
        	console.log(checkLogin);
           if(checkLogin==null){
              //后台没有返回数据,跳转到错误页面
           }else if(checkLogin.result==true){
              //登陆成功,页面跳转到登陆后的页面
              $location.path("/blogList");
           }else{
              //登陆失败,更改密码提示为密码错误,并清空密码
              $scope.isPwd=checkLogin.message;
              $scope.isPwdId="p3";
              document.getElementById("logPwd").value="";
           }
        });
    }

    
	//$watch方法可以实时监控一个对象的变化,当传入的对象,如user有任何变化时,就会执行里边的function
	$scope.$watch('user', function() {
		$scope.isLogin();
	}, true);
}])
user.json数据:

{
	"result":true,
	"message":"密码有误"
}


login.css代码:

/*
公共样式控制
 */
body{
	background:#E8E5E5;
	margin:0;
}
hr{
	width:100%;
}
/*
登陆页面样式控制
 */
#logdiv1{
	width:50%;
	height: 250px;
	background-color: #C2BEBE;
	font-size: 32px;
	padding-top: 40px;
	margin: 72px;
}
#logdiv1 input{
	font-size: 20px;
}
#logdiv1 .p1{
	font-size: 16px;
	display:inline;
	color: red;
	margin-left: 10px;
}
#logdiv1 #p1{
	color: red;
}
#logdiv1 #p2{
	color: green;
}
#logdiv1 #p3{
	color: red;
}
#logdiv1 #p4{
	color: green;
}
/*
导航栏样式控制
 */
#naDiv{
	font-size: 42px;
	width: 100%;
	height: 50px;
	background-color: #181717;
	color: red;
	font-family: 黑体;
}
/*
底部固定div样式控制
 */
#footDiv{
	background-color: #181717;
	width:100%;
	height:125px;
	font-size: 18px;
	margin: 0px;
	color: #71EAF0;
}

.bonInput{
	font-size: 28px;
}

效果图:





目录
相关文章
|
2月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
35 0
|
2月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)(上)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
36 0
|
2月前
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
29天前
|
缓存 负载均衡 NoSQL
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
41 1
|
2月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)(下)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
37 0
|
2月前
|
前端开发 API UED
AngularJS的$http服务:深入解析与进行HTTP请求的技术实践
【4月更文挑战第28天】AngularJS的$http服务是核心组件,用于发起HTTP请求与服务器通信。$http服务简化了通信过程,通过深入理解和实践,能构建高效、可靠的前端应用。
|
2月前
|
缓存 前端开发
HTTP协议学习
HTTP协议学习
28 0
node中的fs模块和http模块的学习
node中的fs模块和http模块的学习
|
2月前
|
缓存 Android开发 数据安全/隐私保护
android开发,使用kotlin学习HTTP访问网络
android开发,使用kotlin学习HTTP访问网络
124 0
|
7月前
|
缓存 安全 前端开发
【HTTP学习】
【HTTP学习】