angularjs中$http、$location、$watch及双向数据绑定学习实现简单登陆验证
使用$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/> ©版权所有: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;
}
效果图:
angularjs中$http、$location、$watch及双向数据绑定学习实现简单登陆验证的更多相关文章
- angularJs初体验,实现双向数据绑定!使用体会:比较爽
使用初体验:ng 双向数据绑定: 最简单的双向数据绑定:(使用默认模块控制) <body ng-app> <input type="text" ng-model= ...
- JavaScript实现简单的双向数据绑定
什么是双向数据绑定 双向数据绑定简单来说就是UI视图(View)与数据(Model)相互绑定在一起,当数据改变之后相应的UI视图也同步改变.反之,当UI视图改变之后相应的数据也同步改变. 双向数据绑定 ...
- 《AngularJS权威教程》中关于指令双向数据绑定的理解
在<AngularJS权威教程>中,自定义指令和DOM双向数据绑定有一个在线demo,网址:http://jsbin.com/IteNita/1/edit?html,js,output,具 ...
- AngularJS入门心得2——何为双向数据绑定
前言:谁说Test工作比较轻松,最近在熟悉几个case,差点没疯.最近又是断断续续的看我的AngularJS,总觉得自己还是没有入门,可能是自己欠前端的东西太多了,看不了几行代码就有几个常用函数不熟悉 ...
- angularJs:双向数据绑定
示例1 <!DOCTYPE html> <html ng-app> <head> <meta charset="UTF-8" /> ...
- Vue、AngularJS 双向数据绑定解剖
数据与视图的绑定与同步,最终体现在对数据的读写处理过程中,也就是 Object.defineProperty() 定义的数据 set.get 函数中.Vue 中对于的函数为 defineReactiv ...
- AngularJS双向数据绑定
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- AngularJS之双向数据绑定,class绑定
之前一直都是用vue来完成一些日常开发,初入AngularJS,记录一些日常开发遇到的问题. 1.双向数据绑定 AngularJS与vue的区别在于,vue采用的是虚拟DOM,模板文件上绑定的一大堆指 ...
- angularjs探秘<四> 双向数据绑定
双向数据绑定是angularjs的一大特性,这个特性在实际开发中省了不少事儿.之前第二篇提过数据绑定,这一篇从实际开发的案例中具体看下双向数据绑定的便捷. 首先看一个场景: 在 注册/登录 中经常遇到 ...
随机推荐
- ZOJ 1095 Humble Numbers
原题链接 题目大意:定义了一种数字Humble Number,他们的质因数只包含2.3.5.7中的一个或者几个,求第n个这样的数,1<=n<=5842. 解法:一看到这道题又在想DFS了, ...
- ZOJ 1202 Divide and Count
原题链接 题目大意:某人手上有一大批钻石,他同时有一些盒子恰好放下这些钻石,每个盒子可以放一个或多个,问一共有几种方法. 解法:这其实是一道排列与组合计算题,主要是写出组合算法的代码,把计算公式转为程 ...
- 前端攻城狮学习笔记九:让你彻底弄清offset
很多初学者对于JavaScript中的offset.scroll.client一直弄不明白,虽然网上到处都可以看一张图(图1),但这张图太多太杂,并且由于浏览器差异性,图示也不完全正确. 图一 不知道 ...
- CSS Flex弹性布局
关于css3的flex布局,阮一峰老师的文章写的清晰易懂又全面,这里附上链接http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_s ...
- Docker简介
Docker简介 1.容器虚拟化,比传统的虚拟化轻量 2.2013年出现,发展非常迅猛 3.Redhat在6.5版本开始支持docker 4.使用go语言开发,基于apache2.0协议 5.开源原件 ...
- Memcached安装及配置
一.Memcached介绍 1.Memcached是国外社区网站LiveJournal团队开发,通过缓存数据库查询结果,减少数据库访问次数,从而提高动态web站点性能. 2.官方站点http://me ...
- LNMP环境搭建(discuz论坛)
一.操作系统级环境及软件版本 操作系统:CentOS release 6.5 (Final)minimal 内核版本:2.6.32-431.el6.x86_64 MySQL版本:MySQL-5.6.2 ...
- php 倒计时程序
<html> <head> <meta http-equiv="Content-Type" content="text/html; ch ...
- java的nio之:java的nio系列教程之channel的数据交换
在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel(译者注:channel中文常译作通道)传输到另外一个channel. transferFro ...
- jQuery - 设置内容和属性
设置内容 - text().html() 以及 val() 我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容( ...