我由于制作登陆界面,用到了angular-validation,结合ng-cookies,实现记住账户密码的功能。文档是https://github.com/hueitan/angular-validation/blob/master/API.md

该文档挺详细的介绍了angular-validation的使用方式。我因为英文不好,理解的不是很仔细。但是总结了以下的使用经验。

1 首先在模块中注入validation模块

  var app = angular.module("zdwq",["ui.router","ui.bootstrap","ngCookies","validation"]);

2 config.js文件中配置config ,设置验证规则的表达式以及文字

  app.config(['"$validationProvider",function($validationProvider){

    var expression = {
      username:/^1[3|4|5|7|8]\d{9}$/,
      password: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,18}$/,
      required:function(value){
         return !!value       
      }
      };
    var defaultMsg = {
      username:{
        success:"",
        error:"必须是手机号"
      },
      password:{
        success:"",
        error:"长度不能小于8位,不能大于18位"
      }
    };

    $validationProvider.setExpression(expression).setDefaultMsg(defaultMsg);

3  html结构中使用验证

  <form name="loginForm">
    <p class="pr"><input name="username_account" validator="username" type="text" class="login-name" placeholder="请输入账号" ng-model="user.username" valid-method="blur,submit"></p>
    <p class="pr"><input name="password_account" validator="password" type="password" class="login-pwd" placeholder="请输入密码" ng-model="user.password" valid-method="blur,submit"></p>
    <p><label class="remember-label"><i class="db vm login-checkbox" ng-class="user.remembered?'checked':''" alt=""></i><input type="checkbox" hidden class="login-remember" ng-model="user.remembered">记住账号</label></p>
    <p><button class="login-btn" ng-click="submit(loginForm)" validator-submit="loginForm">登录</button></p>
  </form>

  注意 (1) form指定名字,方便ng-click调用函数作为参数使用$validation.validate(form).success(function(){}).error(function(){});

      $scope.submit = function(form){
        $validation.validate(form).success(function(){
          $http.get("data/login.json").then(function(){
            if($scope.user.remembered){
              $cookies.put("zdwq_username",$scope.user.username);
              $cookies.put("zdwq_password",$scope.user.password);
            }
            else{
              if($cookies.get("zdwq_username")) $cookies.remove("zdwq_username");
              if($cookies.get("zdwq_password")) $cookies.remove("zdwq_password");
            }
            $window.sessionStorage.setItem("haslogined",true) // 不关闭页面的情况下,刷新页面状态不变
            alert("登录成功");
            $rootScope.isUserAuth = true;
          },function(){
            alert("登录失败")
          })
        }).error(function(){
          // alert("请输入正确的账号或密码")
        })
      }

    (2)  input 中指定validator,表示使用哪个验证规则,另外input中可指定valid-method 表示采用哪种方式对input使用规则,有watch,blur,submit,submit-only方式,默认是watch。需要在点击登录按钮验证的时候,需要指定submit,submit-only 中的一种方式,但是需要为input指定name(没有限制名字的要求)  .为了取得好看的效果,我制作的页面中使用了blur与submit的组合方式。

angular validation 使用总结的更多相关文章

  1. [Angular2 Form] Style Validation in Angular 2 Forms

    Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...

  2. [Angular2 Form] Display Validation and Error Messaging in Angular 2

    Angular 2’s ngModel provides error objects for each of the built-in input validators. You can access ...

  3. [Angular 2] Validation

    Define a filed should has validation: export class DemoFormSku { myForm: ControlGroup; sku: Abstract ...

  4. [Angular2 Form] Create Radio Buttons for Angular 2 Forms

    Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...

  5. angular : ngModel 内部流程

    angular 1.5 beta link NgModelController provides API for the ngModel directive. The controller conta ...

  6. 创建自定义的 Angular Schematics

    本文对 Angular Schematics 进行了介绍,并创建了一个用于创建自定义 Component 的 Schematics ,然后在 Angular 项目中以它为模板演练了通过 Schemat ...

  7. [转]Angular: Hide Navbar Menu from Login page

    本文转自:https://loiane.com/2017/08/angular-hide-navbar-login-page/ In this article we will learn two ap ...

  8. Angular 2 to Angular 4 with Angular Material UI Components

    Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...

  9. User Authentication with Angular and ASP.NET Core

    User authentication is a fundamental part of any meaningful application. Unfortunately, implementing ...

随机推荐

  1. 团队工作效率分析工具gitstats

    如果你是团队领导,关心团队的开发效率和工作激情:如果你是开源软件开发者,维护者某个repo:又或者,你关心某个开源软件的开发进度,那么你可以试一试gitstats. gitstats 是一个git仓库 ...

  2. Ansible 使用普通用户远程执行playbook

    设置ansible使用普通用户jsxge远程连接执行playbook 1. ansible控制端创建普通用户jsxgecd /homeuseradd jsxgechown -R jsxge.wheel ...

  3. Asp.Net EF查看生成sql(MiniProfiler)

    查看ef生成的sql有很多种方法,这里介绍两种几种的方法 方法1:浏览器直接方法/Home/getsql直接查看sql //方法1:浏览器直接方法/Home/getsql直接查看sql public ...

  4. java获取视频的第一帧

    //------------maven配置文件--------------- <dependency> <groupId>org.bytedeco</groupId> ...

  5. Java知多少(72)文件的随机读写

    Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问.使用这个类,可以跳转到文件的任意位置读写数据.程序可以在随机文件中插入数据,而不会破坏该文件的其他数据.此外,程 ...

  6. pca , nmds , pcoa 图添加分组的椭圆

    对于pca , nmds, pcoa 这些排序分析来说,我们可以从图中看出样本的排列规则,比如分成了几组. 为例样本分组更加的直观,我们可以根据实验设计时的样本分组情况,对属于同一个group的样本添 ...

  7. 零基础 Vue 开发环境搭建 打开运行Vue项目

    [相关推荐]IntellIJ IDEA 配置 Vue 支持 打开Vue项目 所需文件 node.js环境(npm包管理器)(node-v8.11.3-x64.msi)(npmV5.6.0) cnpm ...

  8. Php实现版本比较接口

    版本号格式: A.B.C, 字符串形式 <?php // 强更版本, 客户端版本 $f = '5.8.5'; $c = '5.8.4'; // 字符串分割并转换成整数数组 function Sp ...

  9. asp.net mvc 3.0 知识点整理 ----- (3).HtmlHelper(Html 辅助方法)介绍

    在View视图中,Html的类型是System.Web.Mvc.HtmlHelper<T>, 所有的辅助方法都需要和ModelState交互.那么,ModelState是什么呢?它是模型绑 ...

  10. 15适配器模式Adapter

    一.什么是适配器模式 Adapter模式也叫适配器模式,是构造型模式之一 ,通过Adapter模式可以改变已有类(或外部类)的接 口形式. 二.适配器模式应用场景 在大规模的系统开发过程中,我们常常碰 ...