Angular.js表单以及与Bootatrap的使用

首先从angular.js的目录开始,如下图,知道了我们要学什么,然后再开始有目的的学习与对比。

1、从表达式开始:

ng-app指令初始化一个 AngularJS 应用程序。

ng-init指令初始化应用程序数据。

2、指令

3、模型

 4、$scope作用域

5、控制器

 6、过滤器

。。。

 7、表单

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <meta charset="utf-8" />
    <script src="angular.min.js"></script>
</head>
<body>
    <div ng-app="myApp" ng-controller="formController">
        <form novalidate>
            First Name:<br>
            <input type="text" ng-model="user.firstName"><br>
            Last Name:<br>
            <input type="text" ng-model="user.lastName">
            <br><br>
            <button ng-click="reset()">RESET</button>
        </form>
        <p>form = {{user}}</p>
        <p>master = {{master}}</p>
    </div>

    <script>
         var app = angular.module(‘myApp‘, []);
         app.controller(‘formController‘, function ($scope) {
             $scope.master = {firstName: "果果", lastName: "糖糖"};
             $scope.reset = function() {
               $scope.user = angular.copy($scope.master);
             };
            $scope.reset();
         });
    </script>
</body>
</html>

ng-app指令定义了 AngularJS 应用。

                     ng-controller 指令定义了应用控制器。

                     ng-model 指令绑定了两个 input 元素到模型的 user 对象。

                     formCtrl 函数设置了 master 对象的初始值,并定义了 reset() 方法。

                     reset() 方法设置了 user 对象等于 master 对象。

                     ng-click 指令调用了 reset() 方法,且在点击按钮时调用。

novalidate 属性在应用中不是必须的,但是你需要在 AngularJS 表单中使用,用于重写标准的 HTML5 验证。

8、输入验证

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <meta charset="utf-8" />
    <script src="angular.min.js"></script>
</head>
<body>
    <h2>验证demo</h2>
    <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" novalidate>
        <p>
            用户名:<br>
            <input type="text" name="user" ng-model="user" required>
            <span style="color:black" ng-show="myForm.user.$dirty && myForm.user.$invalid">
            <span ng-show="myForm.user.$error.required">用户名非空</span>
            </span>
        </p>

        <p>
            邮箱:<br>
            <input type="email" name="email" ng-model="email" required>
            <span style="color:black" ng-show="myForm.email.$dirty && myForm.email.$invalid">
                <span ng-show="myForm.email.$error.required">邮箱非空</span>
                <span ng-show="myForm.email.$error.email">请输入正确格式的邮箱</span>
            </span>
        </p>

        <p>
            <input type="submit"ng-disabled="myForm.user.$dirty && myForm.user.$invalid || myForm.email.$dirty && myForm.email.$invalid">
        </p>
    </form>

    <script>
         var app = angular.module(‘myApp‘, []);
         app.controller(‘validateCtrl‘, function($scope) {
           $scope.user = ‘果果‘;
           $scope.email = ‘[email protected]‘;
         });
    </script>
</body>
</html>

展示如下:

HTML 表单属性novalidate用于禁用浏览器默认的验证。

9、Boostrap

<!DOCTYPE html>
<html>
<link href="bootstrap.min.css" rel="stylesheet" />
<script src="angular.min.js"></script>
<body ng-app="myApp" ng-controller="userCtrl">
    <div class="container">
        <h3>表格显示Boostrap和Angular.JS的使用</h3>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>编辑</th>
                    <th>名</th>
                    <th>姓</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="user in users">
                    <td>
                        <button class="btn" ng-click="editUser(user.id)">
                            <span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;编辑
                        </button>
                    </td>
                    <td>{{ user.fName }}</td>
                    <td>{{ user.lName }}</td>
                </tr>
            </tbody>
        </table>

        <hr>
        <button class="btn btn-success" ng-click="editUser(‘new‘)">
            <span class="glyphicon glyphicon-user"></span> 新建用户
        </button>
        <hr>

        <h3 ng-show="edit">新建用户</h3>
        <h3 ng-hide="edit">编辑用户</h3>

        <form class="form-horizontal">
            <div class="form-group">
                <label class="col-sm-2 control-label">名:</label>
                <div class="col-sm-10">
                    <input type="text" ng-model="fName" ng-disabled="!edit" placeholder="请输入您的名">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">姓:</label>
                <div class="col-sm-10">
                    <input type="text" ng-model="lName" ng-disabled="!edit" placeholder="请输入您的姓">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">密码:</label>
                <div class="col-sm-10">
                    <input type="password" ng-model="passw1" placeholder="请输入密码">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">重复密码:</label>
                <div class="col-sm-10">
                    <input type="password" ng-model="passw2" placeholder="请重复输入密码">
                </div>
            </div>
        </form>

        <hr>
        <button class="btn btn-success" ng-disabled="error || incomplete">
            <span class="glyphicon glyphicon-save"></span> 保存
        </button>
    </div>
    <script src="angularDemo.js"></script>
</body>
</html>
angular.module(‘myApp‘, []).controller(‘userCtrl‘, function ($scope) {
    $scope.fName = ‘‘;
    $scope.lName = ‘‘;
    $scope.passw1 = ‘‘;
    $scope.passw2 = ‘‘;
    $scope.users = [
    { id: 1, fName: ‘三‘, lName: "张" },
    { id: 2, fName: ‘四‘, lName: "李" },
    { id: 3, fName: ‘五‘, lName: "王" },
    { id: 4, fName: ‘六‘, lName: "张" },
    { id: 5, fName: ‘七‘, lName: "张" },
    { id: 6, fName: ‘八‘, lName: "张" }
    ];
    $scope.edit = true;
    $scope.error = false;
    $scope.incomplete = false;

    $scope.editUser = function (id) {
        //$(‘.form - horizontal‘).show();
        if (id == ‘new‘) {
            $scope.edit = true;
            $scope.incomplete = true;
            $scope.fName = ‘‘;
            $scope.lName = ‘‘;
        } else {
            $scope.edit = false;
            $scope.fName = $scope.users[id - 1].fName;
            $scope.lName = $scope.users[id - 1].lName;
        }
    };

    $scope.$watch(‘passw1‘, function () { $scope.test(); });
    $scope.$watch(‘passw2‘, function () { $scope.test(); });
    $scope.$watch(‘fName‘, function () { $scope.test(); });
    $scope.$watch(‘lName‘, function () { $scope.test(); });

    $scope.test = function () {
        if ($scope.passw1 !== $scope.passw2) {
            $scope.error = true;
        } else {
            $scope.error = false;
        }
        $scope.incomplete = false;
        if ($scope.edit && (!$scope.fName.length ||!$scope.lName.length ||!$scope.passw1.length || !$scope.passw2.length)) {
            $scope.incomplete = true;
        }
    };

});

时间: 2024-08-10 00:06:45

Angular.js表单以及与Bootatrap的使用的相关文章

angular js 表单验证

1 <!doctype html> 2 <html ng-app="myapp"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style> 7 body{ 8 padding: 50px; 9 font-family: "微软雅黑"; 10 } 11 input{ 12 margi

使用Angular提交表单

使用Angular提交表单 我们准备在之前使用的<script>标签中设置我们的Angular应用.所以删除里面的内容,我们就可以开始了. 设置一个Angular应用 步骤为: 1. 加载Angular 2. 设置module 3. 这是controller 4. 将module和controller应用于HTML 5. 设置双向变量绑定 6. 这是错误和信息 看起来好像是很多内容,但是最终,我们会用非常少的代码,并且看起来会非常简洁.另外,创建带有更多输入更大的表单,也会更容易. Angul

JS表单验证类HTML代码实例

以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮点数验证.日期验证.邮件检查.网址验证.固定电话和手机号码验证.IP地址验证.邮编和QQ号码验证. MSN和身份证验证等. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.

jquery.validation.js 表单验证

jquery.validation.js 表单验证 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 一导入js库 <script src="../js/jquery.js" type="text/javascript"></script> <script src="../js/jq

node.js表单——formidable/////z

node.js表单——formidable node处理表单请求,需要用到formidable包.安装formidable包的命令如下: npm install formidable 安装package的路径分为两种,一种是本地目录,一种是全局目录. npm install xxx -g 命令将模块下载安装到全局目录中. 全局目录可以通过 npm config set prefix "目录路径" 来设置. 通过 npm config get prefix 来获取当前设置的目录. npm

JS表单验证-12个常用的JS表单验证

最近有个项目用到了表单验证,小编在项目完结后的这段时间把常用的JS表单验证demo整理了一下,和大家一起分享~~~ 1. 长度限制 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 6

简单的JS表单验证效果代码

简单的JS表单验证代码:表单验证几乎是不可缺少的,有的表单验证是在后台完成的,有的则是使用JavaScript在在前端完成基本的验证,这样可以有效的减轻服务器的压力,下面就介绍一下JS实现的最简单的表单验证.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://w

js表单提交回调函数

在研究表单的时候发现一个有意思的东西——在表单提交的时候如何保证数据全部提交完毕才会清空? 我们常用的<input type="reset" value="重置" />,或者jquery的$('input[name=xxx]')val(''),直接清空input的value值,都是单纯的直接清空,不会等待数据提交完毕后在清空,所以会有数据传输没完成就清空的情况,怎么解决? 搜索的时候发现一个答案——写一个回调函数,感觉不严谨.先记录下来,以后慢慢研究.

jquery.validate.js表单验证

引用jquery封装好的js文件进行表单验证,提高了Web开发的效率.我写了一个验证的实例给大家展示一下. 实例中包含的验证方法还不全面,如果没有大家想要的可以通过 百度搜索关键:jquery.validate.js表单验证帮助文档来进行查阅. 引入的js文件: <script type="text/javascript" src="js/jquery-1.11.2.js"></script> <script type="te