Categories: AngularJS 教程

AngularJS 表单

本节课我们了解下AngularJS 表单。AngularJS 表单是输入控件的集合。

HTML 控件

以下 HTML input 元素被称为 HTML 控件:

  • input 元素
  • select 元素
  • button 元素
  • textarea 元素

数据绑定

Input 控件使用 ng-model 指令来实现数据绑定。

<input type="text" ng-model="firstname">

通过以上代码应用有了一个名为 firstname 的属性。

它通过 ng-model 指令来绑定到你的应用。

firstname 属性可以在 controller 中使用:

var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
    $scope.firstname = "John";
});

完整DEMO:

<!DOCTYPE html>
<html>
<head>
<title>AngularJS 实例 | Web176教程网web176.com</title>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp" ng-controller="formCtrl">
  <form>
    First Name: <input type="text" ng-model="firstname">
  </form>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
    $scope.firstname = "John";
});
</script>

</body>
</html>

也可以在应用的其他地方使用:

<!DOCTYPE html>
<html>
<head>
<title>AngularJS 实例 | Web176教程网web176.com</title>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="">
  <form>
    First Name: <input type="text" ng-model="firstname">
  </form>
  <h1>你输入的内容为: {{firstname}}</h1>
</div>

<p>修改输出框的内容,显示信息也会跟着变化。</p>

</body>
</html>

Checkbox(复选框)

checkbox 的值为 true 或 false,可以使用 ng-model 指令绑定,它的值可以用于应用中。

复选框选中后显示 h1 标签内容:

<!DOCTYPE html>
<html>
<head>
<title>AngularJS 实例 | Web176教程网web176.com</title>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="">
  <form>
    选中复选框,显示标题:
    <input type="checkbox" ng-model="myVar">
  </form>
  <h1 ng-show="myVar">My Header</h1>
</div>

<p>标题使用了 ng-show 指令,复选框选中后显示 h1 标签内容。</p>


</body>
</html>

单选框

我们可以使用 ng-model 来绑定单选按钮到你的应用中。

单选框使用同一个 ng-model ,可以有不同的值,但只有被选中的单选按钮的值会被使用。

根据选中的单选按钮,显示信息:

myVar 的值可以是 dogs, tuts, 或 cars。

下拉菜单

使用 ng-model 指令可以将下拉菜单绑定到你的应用中。

ng-model 属性的值为你在下拉菜单选中的选项。

根据选中的下拉菜单选项,显示信息:

<!DOCTYPE html>
<html>
<head>
<title>AngularJS 实例 | Web176教程网web176.com</title>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="">

<form>
  选择一个选项:
  <select ng-model="myVar">
    <option value="">
    <option value="dogs">Dogs
    <option value="tuts">Tutorials
    <option value="cars">Cars
  </select>
</form>

<div ng-switch="myVar">
  <div ng-switch-when="dogs">
     <h1>Dogs</h1>
     <p>Welcome to a world of dogs.</p>
  </div>
  <div ng-switch-when="tuts">
     <h1>Tutorials</h1>
     <p>Learn from examples.</p>
  </div>
  <div ng-switch-when="cars">
     <h1>Cars</h1>
     <p>Read about cars.</p>
  </div>
</div>

<p>ng-switch 指令根据下拉菜单的选择结果显示或隐藏 HTML 区域。</p>

</body>
</html>

myVar 的值可以是 dogs, tuts, 或 cars。

HTML 表单

HTML 表单通常与 HTML 控件同时存在。

AngularJS 表单实例

应用程序代码:

<!DOCTYPE html>
<html>
<head>
<title>AngularJS 实例 | Web176教程网web176.com</title>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp" ng-controller="formCtrl">
  <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('formCtrl', function($scope) {
    $scope.master = {firstName: "John", lastName: "Doe"};
    $scope.reset = function() {
        $scope.user = angular.copy($scope.master);
    };
    $scope.reset();
});
</script>
 
</body>
</html>

novalidate 属性是在 HTML5 中新增的。禁用了使用浏览器的默认验证。

实例解析

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

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

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

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

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

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

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

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

6 天 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

3 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

4 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago