来源:自学PHP网 时间:2016-04-21 15:40 作者: 阅读:次
[导读] 简单来说Angular.js是google开发者设计和开发的一套前端开发框架,帮助你简化前端开发的负担。到底能简化到什么程度呢,今天我们来看下,一行代码实现控件验证效果,有木有吓尿?...
如上图所示,我们需要实现如下这些验证功能: 控件都是必输控件 HTML。 <!DOCTYPE html> <html lang="zh-cn" ng-app="ftitApp"> <head> <meta charset="utf-8" /> <title>Demo</title> <link href="/Content/bootstrap.css" rel="stylesheet"/> <script src="/Scripts/angular.js"></script> </head> <body> <div class="container body-content"> <!-- 主要内容区域 --> <div class="row main-content"> <div class="col-md-9"> <!-- 联系我们表单区域 --> <form action="/Contact/Create" method="post" role="form" name="createContactForm" ng-controller="ContactCreateController"> <!-- UserName 您的称呼 --> <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.UserName.$pristine && createContactForm.UserName.$valid, 'has-error' : !createContactForm.UserName.$pristine && createContactForm.UserName.$invalid }"> <label for="UserName">您的称呼*</label> <input type="text" class="form-control" ng-model="userName" name="UserName" autofocus="" required ng-maxlength=30> <div ng-show="!createContactForm.UserName.$pristine && createContactForm.UserName.$valid"> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div> <div ng-show="!createContactForm.UserName.$pristine && createContactForm.UserName.$invalid"> <span class="glyphicon glyphicon-remove form-control-feedback"></span> </div> </div> <!-- UserMail 邮箱地址 --> <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.UserMail.$pristine && createContactForm.UserMail.$valid, 'has-error' : !createContactForm.UserMail.$pristine && createContactForm.UserMail.$invalid }"> <label for="UserMail">邮箱地址*</label> <input type="email" class="form-control" ng-model="userMail" name="UserMail" required ng-maxlength=30> <div ng-show="!createContactForm.UserMail.$pristine && createContactForm.UserMail.$valid"> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div> <div ng-show="!createContactForm.UserMail.$pristine && createContactForm.UserMail.$invalid"> <span class="glyphicon glyphicon-remove form-control-feedback"></span> </div> </div> <!-- Subject 主题 --> <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.Subject.$pristine && createContactForm.Subject.$valid, 'has-error' : !createContactForm.Subject.$pristine && createContactForm.Subject.$invalid }"> <label for="Subject">主题*</label> <input type="text" class="form-control" ng-model="subject" name="Subject" required ng-maxlength=100> <div ng-show="!createContactForm.Subject.$pristine && createContactForm.Subject.$valid"> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div> <div ng-show="!createContactForm.Subject.$pristine && createContactForm.Subject.$invalid"> <span class="glyphicon glyphicon-remove form-control-feedback"></span> </div> </div> <!-- Content 内容 --> <div class="form-group has-feedback" ng-class="{'has-success' : !createContactForm.Content.$pristine && createContactForm.Content.$valid, 'has-error' : !createContactForm.Content.$pristine && createContactForm.Content.$invalid }"> <label for="Content">内容*</label> <textarea cols="4" rows="5" class="form-control" ng-model="content" name="Content" required ng-maxlength=1000></textarea> <div ng-show="!createContactForm.Content.$pristine && createContactForm.Content.$valid"> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div> <div ng-show="!createContactForm.Content.$pristine && createContactForm.Content.$invalid"> <span class="glyphicon glyphicon-remove form-control-feedback"></span> </div> </div> <!-- 提交按钮 --> <div class="form-group"> <div ng-show="createContactForm.$valid"> <input type="image" src="/Content/images/comment_publish_button.png" onsubmit="submit();" value="发布" ng-disabled='!createContactForm.$valid' /> </div> <div ng-show="!createContactForm.$valid"> <img src="/Content/images/invalid_publish_button.png" /> </div> </div> </form> </div> </div> </div> <script src="/Scripts/ftit/ContactCreateController.js"></script> </body> </html> JS代码(真的只有一行哟) ContractCreateController.js var ftitAppModule = angular.module('ftitApp', []); 这样就好啦。几个关键的地方解释一下: ng-class:这个标签用来控制class的值。例如ng-class="{'has-success' : !createContactForm.Content.$pristine}的意思就是,如果!createContactForm.Content.$pristine的值为true,class的值就为has-success。 |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com