AngularJS model
ng-model directive is used to bind application data to HTML controller (input, select, textarea).
ng-model Directive
ng-model
The directive binds the value of an input field to a variable created by AngularJS.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <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> <p>使用 ng-model 指令来绑定输入域的值到控制器的属性。</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
Two-way binding
Two-way binding, when the value of the input field is modified, the value of the AngularJS attribute will also be modified:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> 名字: <input ng-model="name"> <h1>你输入了: {{name}}</h1> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.name = "John Doe"; }); </script> <p>修改输入框的值,标题的名字也会相应修改。</p> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Verify user input
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <form ng-app="" name="myForm"> Email: <input type="email" name="myAddress" ng-model="text"> <span ng-show="myForm.myAddress.$error.email">不是一个合法的邮箱地址</span> </form> <p>在输入框中输入你的邮箱地址,如果不是一个合法的邮箱地址,会弹出提示信息。</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
In the above instance , the prompt information will be displayed when the ng-show
attribute returns true
.
Application status
ng-model
The directive can provide status values (invalid,
dirty, touched, error):
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <form ng-app="" name="myForm" ng-init="myText = 'test@php.cn'"> Email: <input type="email" name="myAddress" ng-model="myText" required> <p>编辑邮箱地址,查看状态的改变。</p> <h1>状态</h1> <p>Valid: {{myForm.myAddress.$valid}} (如果输入的值是合法的则为 true)。</p> <p>Dirty: {{myForm.myAddress.$dirty}} (如果值改变则为 true)。</p> <p>Touched: {{myForm.myAddress.$touched}} (如果通过触屏点击则为 true)。</p> </form> </body> </html>
Run Instance»
Click the "Run Instance" button to view online Example
CSS Classes
ng-model
directive provides CSS classes for HTML elements based on their state:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <style> input.ng-invalid { background-color: lightblue; } </style> </head> <body> <form ng-app="" name="myForm"> 输入你的名字: <input name="myName" ng-model="myText" required> </form> <p>编辑文本域,不同状态背景颜色会发送变化。</p> <p>文本域添加了 required 属性,该值是必须的,如果为空则是不合法的。</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
##ng The -model directive adds/removes the following classes based on the state of the form field:
- ng-empty
- ng-not-empty
- ng-touched
- ng-untouched
- ng-valid
- ng-invalid
- ng-dirty
- ng-pending
- ng-pristine