美文网首页
ng-Message表单验证(一)

ng-Message表单验证(一)

作者: 遥望泰山 | 来源:发表于2016-06-08 10:01 被阅读0次
    1. angular-messages进行安装
    2. 引用:angular-messages.js
    3. 依赖:angular.module(‘app‘,[‘ngMessages‘])

    index.html中

    <html>
    <head>
        <meta charset="UTF-8">
        <title>Angular JS Forms</title>
        <script src="angular/angular.min.js"></script>
        <script src = bower-angular-messages-master/bower-angular-messages-master/angular-messages.min.js></script>
    
    </head>
    <body>
    <h2>验证实例</h2>
    
    <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" novalidate>
        <p>用户名<br>
        <input type="text" name="user" ng-minlength="3" ng-maxlength="10" ng-model="user"  required/>
        <!--<span  style="color:red"  ng-show="myForm.user.$dirty&&myForm.user.$invalid">-->
        <!--<span ng-show="myForm.user.$error.required">用户名是必须的</span>-->
    
        <div ng-messages="myForm.user.$error" ng-if="myForm.user.$dirty">
            <div ng-message="required">必填项</div>
            <div ng-message="minlength">字符太短小于3</div>
            <div ng-message="maxlength">字符太长大于20</div>
    
    <div ng-include="'error.html'"></div>  //也可以这样用
    </div>
    
    </form>
    <script>
        var app = angular.module('myApp', ['ngMessages']);
        app.controller('validateCtrl', function($scope) {
            $scope.user = '';
           
        });
    </script>
    </body>
    </html>
    

    在error.html中

    <div ng-message="required">必填项</div>
    <div ng-message="email">邮件格式不对</div>
    <div ng-message="minlength">字符太短小于3</div>
    <div ng-message="maxlength">字符太长大于20</div>
    

    相关文章

      网友评论

          本文标题:ng-Message表单验证(一)

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