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

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


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

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

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

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

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


app.js代码

[javascript]  view plain  copy
 
  1. var app=angular.module('contactsApp',['ui.router']);  
  2. app.config(function($stateProvider,$urlRouterProvider){  
  3.     $urlRouterProvider.otherwise('/login');  
  4.         $stateProvider.state('login', {  
  5.             url: "/login",  
  6.             views: {  
  7.                 'view': {  
  8.                     templateUrl: 'views/login.html',  
  9.                     controller: 'loginCtr'  
  10.                 }  
  11.             }  
  12.         });  
  13. });  
首页contacts.html代码:

[html]  view plain  copy
 
  1. <!DOCTYPE html>  
  2. <html ng-app="contactsApp">  
  3. <head>  
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.   <script src="./angular-1.4.0-rc.2/angular.js"></script>  
  6.   <link rel="stylesheet" href="./css/login.css"/>  
  7.   <script src='./angular-1.4.0-rc.2/angular-ui-router.js'></script>  
  8.   <script src='./scripts/app.js'></script>  
  9.   <script src="./scripts/controllers/loginCtr.js"></script>  
  10.   <script src="./scripts/controllers/blogCtr.js"></script>  
  11. </head>  
  12. <body>  
  13.   <center>  
  14.   <!--顶部标题-->  
  15.   <div id="naDiv">  
  16.      My  ContactsSystem  
  17.   </div>  
  18. </br>  
  19.   </center>  
  20.   <!--使用ui-rourer控制页面之间的切换-->  
  21.   <div ui-view="view"></div>  
  22.  <center>  
  23. <!--页面底部固定内容-->  
  24. <div id="footDiv">  
  25. <hr/>  
  26. 链接:  
  27. <a href="http://www.baidu.com">百度</a>  
  28.    
  29. <a href="http://www.sina.com.cn">新浪</a>  
  30.    
  31. <a href="http://blog.csdn.net/tuzongxun">CSDN</a>  
  32.    
  33. <hr/>  
  34. &copy;版权所有:aaaa  
  35. <p>  
  36.     联系我:  
  37.     <a href="mailto:tuzongxun123@163.com">tuzongxun123@163.com</a>  
  38. </p>  
  39. </center>  
  40. </div>  
  41. </body>  
  42. </html>  



login.html代码:

[html]  view plain  copy
 
  1. <center>  
  2. <div id="logdiv1">  
  3.     userName:<input type="text" ng-model="user.userName"/><p class="p1" id="{{isUserId}}">{{isUserName}}</p>  
  4.     </br>  
  5.     </br>  
  6.     password:<input id="logPwd" type="password" ng-model="user.userPwd"/><p class="p1" id="{{isPwdId}}">{{isPwd}}</p>  
  7.     </br>  
  8.     </br>  
  9.     <input class="bonInput" type="button" value="login" ng-click="Login(user);" ng-disabled="login">  
  10.         
  11.     <input class="bonInput" type="button" value="regist">  
  12. </div>  
  13. </center>  

loginCtr.js代码:

[html]  view plain  copy
 
  1. angular.module('contactsApp')  
  2. .controller('loginCtr', ['$scope','$http','$location', function($scope,$http,$location){  
  3.     //模拟后台用户名和密码数据  
  4.     var userNames=['aaa','bbb','ccc'];  
  5.     var userPwd='123';  
  6.     //用户名和密码初始提示文字  
  7.     $scope.isUserName='请输入用户名';  
  8.     $scope.isPwd='请输入密码';  
  9.     //是否启用登陆按钮  
  10.     var isLogin1=false;  
  11.     var isLogin2=false;  
  12.     $scope.login=true;  
  13.     //用户验证,随输入实时提醒,这里只判断用户名是否存在和空值,还可以验证输入字符的长度、格式等  
  14.     $scope.existUser=function(){  
  15.         for(var i=0;i<userNames.length;i++){  
  16.         if($scope.user!=null&&$scope.user.userName!=''&&userNames[i]==$scope.user.userName){  
  17.            isLogin1=true;  
  18.            $scope.isUserName='用户名存在';  
  19.            //用户名存在时,通过更改id,更改提示文字的颜色为绿色  
  20.            $scope.isUserId="p2";  
  21.            break;  
  22.         }else if($scope.user==null||$scope.user.userName==''){  
  23.            isLogin1=false;  
  24.            $scope.isUserName='请输入用户名';  
  25.            $scope.isUserId="p1";  
  26.            //document.getElementById('p1').style.color='green';  
  27.         }else{  
  28.            isLogin1=false;  
  29.            $scope.isUserName='用户名不存在';  
  30.            $scope.isUserId="p1";  
  31.            //document.getElementById('p1').style.color='green';  
  32.         }  
  33.         }  
  34.     }  
  35.     //legal:合法,判断密码输入是否合法,这里只判断长度,还可以正则表达式匹配,限定数字和字母  
  36.     $scope.legalPwd=function(){  
  37.         if($scope.user!=null&&$scope.user.userPwd!=null&&$scope.user.userPwd.length>=6&&$scope.user.userPwd.length<=15){  
  38.            isLogin2=true;  
  39.            //密码格式正确时,修改id,使提示文字颜色变绿  
  40.            $scope.isPwdId="p4";  
  41.            $scope.isPwd="密码格式正确";  
  42.         }else{  
  43.            isLogin2=false;  
  44.            $scope.isPwdId="p3";  
  45.            $scope.isPwd="密码格式不正确";  
  46.         }  
  47.     }  
  48.   
  49.     //调用用户验证和密码验证方法,判断登陆按钮是否可用  
  50.     $scope.isLogin=function(){  
  51.         $scope.existUser();  
  52.         $scope.legalPwd();  
  53.         if(isLogin1&&isLogin2){  
  54.            //当用户名存在,并且密码格式正确的时候,启用登陆按钮  
  55.            $scope.login=false;  
  56.         }else{  
  57.            $scope.login=true;  
  58.         }  
  59.     }  
  60.     //登陆跳转,传数据到后台判断用户名和密码是否匹配,模拟http请求  
  61.     $scope.Login=function(user){  
  62.         //模拟http请求,从后台返回数据  
  63.         $http.get("./json/user.json",user).success(function(checkLogin){  
  64.             console.log(checkLogin);  
  65.            if(checkLogin==null){  
  66.               //后台没有返回数据,跳转到错误页面  
  67.            }else if(checkLogin.result==true){  
  68.               //登陆成功,页面跳转到登陆后的页面  
  69.               $location.path("/blogList");  
  70.            }else{  
  71.               //登陆失败,更改密码提示为密码错误,并清空密码  
  72.               $scope.isPwd=checkLogin.message;  
  73.               $scope.isPwdId="p3";  
  74.               document.getElementById("logPwd").value="";  
  75.            }  
  76.         });  
  77.     }  
  78.   
  79.       
  80.     //$watch方法可以实时监控一个对象的变化,当传入的对象,如user有任何变化时,就会执行里边的function  
  81.     $scope.$watch('user', function() {  
  82.         $scope.isLogin();  
  83.     }, true);  
  84. }])  
user.json数据:

[html]  view plain  copy
 
  1. {  
  2.     "result":true,  
  3.     "message":"密码有误"  
  4. }  


login.css代码:

[css]  view plain  copy
 
  1. /* 
  2. 公共样式控制 
  3.  */  
  4. body{  
  5.     background:#E8E5E5;  
  6.     margin:0;  
  7. }  
  8. hr{  
  9.     width:100%;  
  10. }  
  11. /* 
  12. 登陆页面样式控制 
  13.  */  
  14. #logdiv1{  
  15.     width:50%;  
  16.     height250px;  
  17.     background-color#C2BEBE;  
  18.     font-size32px;  
  19.     padding-top40px;  
  20.     margin72px;  
  21. }  
  22. #logdiv1 input{  
  23.     font-size20px;  
  24. }  
  25. #logdiv1 .p1{  
  26.     font-size16px;  
  27.     display:inline;  
  28.     colorred;  
  29.     margin-left10px;  
  30. }  
  31. #logdiv1 #p1{  
  32.     colorred;  
  33. }  
  34. #logdiv1 #p2{  
  35.     colorgreen;  
  36. }  
  37. #logdiv1 #p3{  
  38.     colorred;  
  39. }  
  40. #logdiv1 #p4{  
  41.     colorgreen;  
  42. }  
  43. /* 
  44. 导航栏样式控制 
  45.  */  
  46. #naDiv{  
  47.     font-size42px;  
  48.     width100%;  
  49.     height50px;  
  50.     background-color#181717;  
  51.     colorred;  
  52.     font-family: 黑体;  
  53. }  
  54. /* 
  55. 底部固定div样式控制 
  56.  */  
  57. #footDiv{  
  58.     background-color#181717;  
  59.     width:100%;  
  60.     height:125px;  
  61.     font-size18px;  
  62.     margin0px;  
  63.     color#71EAF0;  
  64. }  
  65.   
  66. .bonInput{  
  67.     font-size28px;  
  68. }  

效果图:

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

你可能感兴趣的:(angularjs中$http、$location、$watch及双向数据绑定学习实现简单登陆验证)