AngularJs用戶(hù)登錄問(wèn)題處理(交互及驗(yàn)證、阻止FQ處理)
本文介紹了AngularJs用戶(hù)登錄的交互及驗(yàn)證、阻止FQ處理,具體如下
1. 靜態(tài)頁(yè)面搭建及ng的form表單驗(yàn)證實(shí)現(xiàn):
<div class="register-frame-all"> <div class="register-frame"> <div class="register-msg"> <i></i> <form name="loginForm" ng-submit="loginAction()"> <div class="form-group"> <div class="input-group"> <span class="input-group-addon register-user"></span> <input autocomplete="off" type="number" class="form-control" placeholder="請(qǐng)輸入手機(jī)號(hào)" required ng-model="loginData.loginName" name="loginName"> </div> <div class="input-group"> <span class="input-group-addon register-pwd"></span> <input type="password" class="form-control" placeholder="請(qǐng)輸入密碼" required ng-model="loginData.pwd" name="pwd"> </div> <button type="submit" class="btn btn-block btn-danger" ng-disabled="!( (loginForm.loginName.$valid) && (loginForm.pwd.$valid) )">登錄</button> <em></em> </div> </form> </div> <div class="register-pic" ng-style="registerRnum"></div> </div> </div>
2. 定義用戶(hù)登錄的控制器,在控制器中使用http服務(wù)處理登錄接口:
$http({ url:G.apiUrl_dl+'loginByPhone', method:'post', data:{ 'phone':loginName, 'pwd':pwd }, headers:{'Content-Type':'application/x-www-form-urlencoded'}, transformRequest: function(obj) { var str = []; for(var p in obj){ str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); } }).success(function(data){ // 登錄成功后的操作...18 19 });
3. 如果登錄成功后將用戶(hù)的數(shù)據(jù)保存到cookie或session中 用$state服務(wù)進(jìn)行跳轉(zhuǎn)到指定的頁(yè)面中:
// 登錄成功 if($scope.loginActionData.token){ sessionStorage.setItem("token", $scope.loginActionData.token); sessionStorage.setItem("tsname", $scope.loginActionData.name); sessionStorage.setItem("rights", $scope.loginActionData.rights); sessionStorage.setItem("userId", $scope.loginActionData.userId); sessionStorage.setItem("departmentsId", $scope.loginActionData.departmentsId); sessionStorage.setItem("departmentsName", $scope.loginActionData.departmentsName); $state.go('index'); }else{ // 登錄失敗的彈框提示 $('#loginAction').modal('show'); }
4. 接下來(lái)就是防止用戶(hù)跳過(guò)登錄頁(yè)面通過(guò)其他方法(如在地址欄直接輸出地址進(jìn)入頁(yè)面) 的防FQ操作:
這個(gè)方法的操作我放在之前曾經(jīng)說(shuō)過(guò)得控制器最先執(zhí)行的run方法中執(zhí)行,每次進(jìn)到一個(gè)頁(yè)面之前都會(huì)進(jìn)行檢查該用戶(hù)是否合法登錄,如果不是合法登錄我們將會(huì)讓他跳轉(zhuǎn)到登錄頁(yè)面
angular.module.run(['$rootScope','$state',function($rootScope,$state){ $rootScope.$on('$stateChangeStart',function(event,toState){ // 防止FQ if(!(sessionStorage.getItem("token")))$state.go('register'); }); }]);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angularjs 表單密碼驗(yàn)證自定義指令實(shí)現(xiàn)代碼
這篇文章主要介紹了angularjs 表單密碼驗(yàn)證自定義指令實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-10-10Angular?項(xiàng)目路徑添加指定的訪問(wèn)前綴
這篇文章主要為大家介紹了Angular?項(xiàng)目路徑添加指定的訪問(wèn)前綴方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03詳解Angular項(xiàng)目中共享模塊的實(shí)現(xiàn)
本文主要介紹了Angular的共享模塊的實(shí)現(xiàn),對(duì)此感興趣的同學(xué),可以實(shí)驗(yàn)一下2021-05-05Angular 4.x 動(dòng)態(tài)創(chuàng)建表單實(shí)例
本篇文章主要介紹了Angular 4.x 動(dòng)態(tài)創(chuàng)建表單實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Angular8升級(jí)至Angular13遇到的問(wèn)題解決
這幾天升級(jí)公司的一個(gè)Angular項(xiàng)目遇到了一些問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Angular8升級(jí)至Angular13遇到的問(wèn)題解決,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01AngularJS自定義插件實(shí)現(xiàn)網(wǎng)站用戶(hù)引導(dǎo)功能示例
這篇文章主要介紹了AngularJS自定義插件實(shí)現(xiàn)網(wǎng)站用戶(hù)引導(dǎo)功能,結(jié)合實(shí)例形式分析了AngularJS自定義插件的實(shí)現(xiàn)步驟與相關(guān)功能技巧,需要的朋友可以參考下2016-11-11