angularjs 表单
angularjs 表单是输入控件的集合。
html 控件
以下 html input 元素被称为 html 控件:
input 元素
select 元素
button 元素
textarea 元素
html 表单
html 表单通常与 html 控件同时存在。
angularjs 表单实例
first name:
last name:
form = {"firstname":"john","lastname":"doe"}
master = {"firstname":"john","lastname":"doe"}
应用程序代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://www.51sjk.com/Upload/Articles/1/0/268/268341_20210708023636324.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>
运行结果:
first name:
last name:
form = {"firstname":"john","lastname":"doe"}
master = {"firstname":"john","lastname":"doe"}
注意: novalidate 属性是在 html5 中新增的。禁用了使用浏览器的默认验证。
实例解析
ng-app 指令定义了 angularjs 应用。
ng-controller 指令定义了应用控制器。
ng-model 指令绑定了两个 input 元素到模型的 user 对象。
formctrl 函数设置了 master 对象的初始值,并定义了 reset() 方法。
reset() 方法设置了 user 对象等于 master 对象。
ng-click 指令调用了 reset() 方法,且在点击按钮时调用。
novalidate 属性在应用中不是必须的,但是你需要在 angularjs 表单中使用,用于重写标准的 html5 验证。
以上就是对angularjs 表单资料的整理,后续继续补充,希望能帮助编程的同学。
亖呉?盀