ng-messages 的$dirty 使用

用 ng-show 来显示错误信息的时候需要写很多的代码,找了一个插件 ng-messages。使用方法下:

js 代码中加入依赖

var myApp = angular.module(‘myApp‘, [
    "restangular",
    ‘ui.router‘,
    ‘ngMessages‘,
    ...

html 中添加 js 文件

<script src="bower_components/angular-messages/angular-messages.js"></script>

下面是一个注册页面的示例

<div class="container" ng-controller="registerController">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
            <div class="panel-heading">
            </div>
                <div class="panel-body">
                    <div role="form" class="form-horizontal">
                        <form name="register_form" novalidate >
                            <div class="form-group">
                                <lable class="col-sm-2 control-label">Email:</lable>
                                <div class="col-sm-10">
                                    <input class="form-control"
                                    ng-model="userinfo.email"
                                    placeholder="请输入电子邮箱作为用户名" name="email" type="email" autofocus required minlength="6"  ng-maxlength="20"/ >
                                </div>
                            </div>

                            <div ng-messages="register_form.email.$error" ng-if="register_form.email.$dirty" ng-messages-multiple ng-messages-include="pages/error.html">
                            </div>

                            <div class="form-group">
                                <lable class="col-sm-2 control-label">Password:</lable>
                                <div class="col-sm-10">
                                  <input class="form-control"
                                  ng-model="userinfo.password"
                                  placeholder="请输入密码" name="password" type="password" required ng-minlength="6" ng-maxlength="20"/>
                                </div>
                            </div>

                            <div ng-messages="register_form.password.$error" ng-if="register_form.password.$dirty" ng-messages-multiple ng-messages-include="pages/error.html">
                            </div>

                            <div class="form-group">
                                <lable class="col-sm-2 control-label"></lable>
                                <div class="col-sm-10">
                                  <input class="form-control" placeholder="请再次输入密码" name="password-again" type="password"  required ng-minlength="6" ng-maxlength="20"/>
                                </div>
                            </div>

                            <div ng-messages="register_form.password-again.$error" ng-if="register_form.password-again.$dirty" ng-messages-multiple ng-messages-include="pages/error.html">
                            </div>

                            <!-- <lable class="col-sm-2 control-label"></lable> -->
                            <button class="btn btn-success col-sm-offset-2" ng-disabled="register-form.$invalid">注册</button>
                        </form>
                    </div>
                </div>

                <div class="panel-footer">
                    <div class="alert alert-error">
                        {{userinfo | json}}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

error.html


<div class="col-sm-offset-2"><p class="text-danger" ng-message="required">* 此项为必填项</p></div>
<div class="col-sm-offset-2"><p class="text-danger" ng-message="minlength">* 最小6个字符 </p></div>
<div class="col-sm-offset-2"><p class="text-danger" ng-message="maxlength">* 最大20个字符 </p></div>
<div class="col-sm-offset-2"><p class="text-danger" ng-message="email">* 请输入 email 地址</p></div>

如果只是使用 ng-messages="register_form.email.$error 来判断是否使用 message 会直接显示出来,如果在后面加上 ng-if="register_form.email.$dirty" 就 ok 啦。

给自己做个记录!

时间: 2024-12-06 23:01:46

ng-messages 的$dirty 使用的相关文章

Distributed Cache Coherence at Scalable Requestor Filter Pipes that Accumulate Invalidation Acknowledgements from other Requestor Filter Pipes Using Ordering Messages from Central Snoop Tag

A multi-processor, multi-cache system has filter pipes that store entries for request messages sent to a central coherency controller. The central coherency controller orders requests from filter pipes using coherency rules but does not track complet

Scaling the Messages Application Back End 【转】

11年的blog. Facebook Messages seamlessly integrates many communication channels: email, SMS, Facebook Chat, and the existing Facebook Inbox. Combining all this functionality and offering a powerful user experience involved building an entirely new infr

[Ng]Angular应用点概览

----------------------------------------------------------------------------------------------------- 1. 使用模块化写法. var app = angular.module('myApp', []); app.controller('TextController', function($scope) { $scope.txt = {'title':'some txt'}; }); ng-mod

嘿!@野兽,你的ng api 掉了 - - angular.forEach

@野兽的 ng api 学习 -- angular.forEach angular.forEach 调用迭代器函数取每一项目标的集合,它可以是一个对象或数组.迭代器函数与迭代器(value.key)一起调用,其中值是一个对象属性或数组元素的值,而数组元素是对象属性的关键或数组元素索引.为函数指定一个可选的上下文. 格式:angular.forEach(obj,iterator,[context]); obj:遍历的对象 iterator:迭代器 [content]: 对象为迭代器函数的上下文(t

dirty memory

关于各种内存的解释,来源于stackoverflow Wired : This refers to kernel code and such. Memory that should not   ever be moved out of the RAM. Also know as resident memory. Shared : Memory that is shared between two or more processes. Both   processes would show thi

java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFil

项目启动时报错:java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2的jar包版本号为2.1.8,所以类名是没有错误的,而jar包确实加入进去了.jar包加入的方式是通过user library. 问题解决的办法是:将jar包拷贝到WEB-INF下lib文件夹目录,重新启动tomcat问题解决. ================

ng build --aot --prod生成文件报错

Cannot read property 'create' of undefined when building with --prod or --aot 解决方案:改变zone.js的版本号为0.8.5: 然后删除dist ng_modules,然后重装下就可以正常使用了. /*更新angular-cli*/npm uninstall -g angular-cli npm cache clean npm install -g @angular/[email protected] /*更新引用r

ng $scope与$rootScope的关系

$scope与$rootScope的关系:①不同的控制器之间 是无法直接共享数据②$scope是$rootScope的子作用域对象$scope的id是随着控制器的加载顺序依次递增,$rootScope的id是1 ③不同控制器之间如何通信??1.借助于$rootScope2.既然子作用域对象可以调用父作用域对象的值或者方法,就可以通过控制器之间的嵌套来实现通信3.事件父->子$scope.$broadcast('eventName',args) 子->父$scope.$emit('eventNa

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

1.先建立一个项目2.在WebRoot中建立一个jsp,名为index.jsp,在里面写一句话 <body> hello struts2 <br> </body> 3.在后台写入代码 public class T { org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter f; } 4.在struts.xml中写入 <constant name="struts.devMo

嘿!@野兽,你的ng api 掉了 - - angular.uppercase和angular.lowercase

@野兽的 ng api 学习 -- angular.uppercase和angular.lowercase angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = "ABCabc"; var upperCase = angular.uppercase(str);//ABCABC angular.lowercase 将指定的字符串转换成小写 格式:ang