ng-model指令作用是绑定HTML表单元素到AngularJS应用程序数据中,即$scope变量中.
语法:
<tag ng-model="name"></tag>
其中tag可以是input,select,textarea
如:
<div ng-app="myApp" ng-controller="myCtrl"> Name: <input ng-model="name"> </div> <script> var app = angular.module("myApp",[]); app.controller('myCtrl',function($scope){ $scope.name = "tina" }) </script>
ng-model可实现数据双向绑定,上例是通过控制$scope变量中的name值,input元素只会跟着name值变化而变化;除此之外也可以让name值由input输入值来控制.如:
<div ng-app="myApp" ng-controller="myCtrl"> Name: <input ng-model="name"> <h1>You entered: {{name}}</h1> </div> <script> var app = angular.module('myApp',[]); app.controller('myCtrl',function($scope) { $scope.name = "John Doe"; }); </script>