AngularJS 模型
ng-model 指令
-
ng-model 指令用于綁定應(yīng)用程序數(shù)據(jù)到 HTML 控制器(input, select, textarea)的值。
<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>
-
雙向綁定
<div ng-app="myApp" ng-controller="myCtrl"> 名字: <input ng-model="name"> <h1>你輸入了: {{name}}</h1> </div>
-
驗證用戶輸入
<form ng-app="" name="myForm"> Email: <input type="email" name="myAddress" ng-model="text"> <span ng-show="myForm.myAddress.$error.email">不是一個合法的郵箱地址</span> </form>
-
響應(yīng)狀態(tài)
ng-model 指令可以為應(yīng)用數(shù)據(jù)提供狀態(tài)值(invalid, dirty, touched, error):<form ng-app="" name="myForm" ng-init="myText = 'test@runoob.com'"> <p> Email: <input type="email" name="myAddress" ng-model="myText" required> </p> <h1>狀態(tài)</h1> {{myForm.myAddress.$valid}} {{myForm.myAddress.$dirty}} {{myForm.myAddress.$touched}} </form>
-
css 類
ng-model 指令基于它們的狀態(tài)為 HTML 元素提供了 CSS 類:<style> input.ng-invalid { background-color: lightblue; } </style> <body> <form ng-app="" name="myForm"> 輸入你的名字: <input name="myAddress" ng-model="text" required> </form>
-
ng-model 指令根據(jù)表單域的狀態(tài)添加或移除
- ng-empty
- ng-not-empty
- ng-touched
- ng-untouched
- ng-valid
- ng-dirty
- ng-pending
- ng-pristine