美文网首页
表单状态及验证

表单状态及验证

作者: hello高world | 来源:发表于2017-01-13 22:55 被阅读0次

1. 理论知识

  • $invalid: 一旦表单没有验证通过,AngularJS就会为表单设置为$invalid属性
  • 验证器(用来验证表单)
1. ng-required
2. ng-minlength
3. type="email"
4. type="url"
5. type="date"
6. 等等
  • 验证后的结果状态(boolean类型)。一般是$error.email或者$error.minlength
1. $invalid
2. email
3. minlength
4. 等等
  • 验证后的结果状态 对应的 css class
1. $invalid--> ng-invalid
2. email --> ng-valid-email / ng-invalid-email
3. minlength --> ng-valid-minlength / ng-invalid-minlength

2. error_msg.html

  <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>错误消息</title>
    <meta name="keywords" content="错误消息" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" name="viewport">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">

    <script src="js/common/angular.min.js"></script>
    <script src="js/common/angular-ui-router.min.js"></script>

    <style>
        .username.ng-dirty.ng-invalid-minlength, .email.ng-dirty.ng-invalid-email{
            background-color: lightpink;
        }
        .username.ng-valid, .email.ng-valid{
            background-color: green;
        }
    </style>
</head>

<body  ng-app="notesApp" ng-controller="MainCtrl as ctrl">
<div>
    <form ng-submit="ctrl.submit()" name="myform">
        username: <input type="text"
               class="username"
               name="uname"
               ng-model="ctrl.username"
               ng-minlength="4">
        <span ng-show="myform.uname.$error.minlength">
                最小长度为4</span>
        <br>

        email: <input class="email"
               name="email"
               ng-model="ctrl.email"
               type="email">
        <span ng-show="myform.email.$error.email">
                邮箱格式出错</span>
        <br>

        password: <input type="password"
               ng-model="ctrl.password">
        <br>

        <button ng-click="ctrl.change()">重置为初始数据</button>
        <input type="submit"
               value="submit"
               ng-disabled="myform.$invalid">
    </form>
</div>

</body>
<script type="text/javascript">
    angular.module('notesApp',[])
        .controller('MainCtrl', [function(){
            var self = this;
            self.username = 'tinygao';
            self.email = 'tinygao@163.com';
            self.password = 'helloworld';
            console.log('Main Ctrl');
            self.change = function() {
                self.username = 'tinygao';
                self.email = 'tinygao@163.com';
                self.password = 'helloworld';
            };

            self.submit = function() {
                console.log('user click submit with ', self.username, self.password);
            };
        }]);
</script>
</html>

3. 展示页面

错误email格式,无法提交 正确格式,可以点击提交

4. 解析

  • 表单校验器: ng-minlength="4"
  • 校验状态结果: myform.uname.$error.minlength
  • css对应状态结果表示:
    1. .username.ng-dirty.ng-invalid-minlength
    2. .username.ng-valid

相关文章

网友评论

      本文标题:表单状态及验证

      本文链接:https://www.haomeiwen.com/subject/zpasbttx.html