AngularJS ng-model 指令
ng-model 指令用于绑定应用程序数据到 HTML 控制器(input, select, textarea)的值。
ng-model 指令
ng-model
指令可以将输入域的值与 AngularJS 创建的变量绑定。
AngularJS 实例
<div ng-app="myApp" ng-controller="myCtrl">
名字: <input ng-model="name">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
名字: <input ng-model="name">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
双向绑定
双向绑定,在修改输入域的值时, AngularJS 属性的值也将修改:
AngularJS 实例
<div ng-app="myApp" ng-controller="myCtrl">
名字: <input ng-model="name">
<h1>你输入了: {{name}}</h1>
</div>
名字: <input ng-model="name">
<h1>你输入了: {{name}}</h1>
</div>
验证用户输入
AngularJS 实例
<form ng-app="" name="myForm">
Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">不是一个合法的邮箱地址</span>
</form>
Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">不是一个合法的邮箱地址</span>
</form>
应用状态
ng-model
指令可以为应用数据提供状态值(invalid, dirty, touched, error):
AngularJS 实例
<form ng-app="" name="myForm" ng-init="myText = 'test@runoob.com'">
Email:
<input type="email" name="myAddress" ng-model="myText" required></p>
<h1>状态</h1>
{{myForm.myAddress.$valid}}
{{myForm.myAddress.$dirty}}
{{myForm.myAddress.$touched}}
</form>
Email:
<input type="email" name="myAddress" ng-model="myText" required></p>
<h1>状态</h1>
{{myForm.myAddress.$valid}}
{{myForm.myAddress.$dirty}}
{{myForm.myAddress.$touched}}
</form>
CSS 类
ng-model
指令基于它们的状态为 HTML 元素提供了 CSS 类:
AngularJS 实例
<style>
input.ng-invalid {
background-color: lightblue;
}
background-color: lightblue;
}
</style>
<body>
<form ng-app="" name="myForm">
输入你的名字:
<input name="myAddress" ng-model="text" required>
</form>
<body>
<form ng-app="" name="myForm">
输入你的名字:
<input name="myAddress" ng-model="text" required>
</form>
ng-model
指令根据表单域的状态添加/移除以下类:
- ng-empty
- ng-not-empty
- ng-touched
- ng-untouched
- ng-valid
- ng-invalid
- ng-dirty
- ng-pending
- ng-pristine
以下是源代码
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>angular model 学习</title>
<!-- <link rel="stylesheet" href="node_modules/.1.6.1@angular/angular.css"> -->
<style>
input.ng-invalid{
font-size: 26px;
color: green;
background-color: lightblue;
}
</style>
</head>
<body>
<div ng-controller="myCtrl" style="border:1px solid #000;">
名字:<input ng-model="name">
<h1>{{name}}</h1>
</div>
<div style="border:1px solid #000;min-height: 100px;">
<form name="myForm">
Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">不是一个合法的邮箱地址</span>
<p>Valid: {{myForm.myAddress.$valid}} (如果输入的值是合法的则为 true)。</p>
<p>Dirty: {{myForm.myAddress.$dirty}} (如果值改变则为 true)。</p>
<p>Touched: {{myForm.myAddress.$touched}} (如果通过触屏点击则为 true)。</p>
</form>
</div>
<div style="border:1px solid #000;min-height: 100px;">
<form name="YourForm">
<p>使用angular的css</p>
<input class="myname" ng-model="name" required />
</form>
</div>
</body>
<script src='node_modules/.1.6.1@angular/angular.js'></script>
<script>
var app=angular.module('myApp',[]);
app.controller('myCtrl',function($scope){
$scope.name="John Doe";
});
</script>
</html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>angular model 学习</title>
<!-- <link rel="stylesheet" href="node_modules/.1.6.1@angular/angular.css"> -->
<style>
input.ng-invalid{
font-size: 26px;
color: green;
background-color: lightblue;
}
</style>
</head>
<body>
<div ng-controller="myCtrl" style="border:1px solid #000;">
名字:<input ng-model="name">
<h1>{{name}}</h1>
</div>
<div style="border:1px solid #000;min-height: 100px;">
<form name="myForm">
Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">不是一个合法的邮箱地址</span>
<p>Valid: {{myForm.myAddress.$valid}} (如果输入的值是合法的则为 true)。</p>
<p>Dirty: {{myForm.myAddress.$dirty}} (如果值改变则为 true)。</p>
<p>Touched: {{myForm.myAddress.$touched}} (如果通过触屏点击则为 true)。</p>
</form>
</div>
<div style="border:1px solid #000;min-height: 100px;">
<form name="YourForm">
<p>使用angular的css</p>
<input class="myname" ng-model="name" required />
</form>
</div>
</body>
<script src='node_modules/.1.6.1@angular/angular.js'></script>
<script>
var app=angular.module('myApp',[]);
app.controller('myCtrl',function($scope){
$scope.name="John Doe";
});
</script>
</html>