AngularJS 在 <input type="text" /> 中实现双向动态绑定十分简单,以下所示:html
<input type="text" ng-model="topic.title" />
只须要用ng-model 与 $scope 中的属性对应,即实现了type=”text” 的双向动态绑定。当 <input type="radio" /> 及 <input type="checkbox" /> 时状况略有不一样:ide
1. <inputtype="radio" /> :spa
<input type="radio" name="person-action" value="go_home" ng-model="person.action" />回家 <input type="radio" name="person-action" value="go_to_school" ng-model="person.action" />回学校
经过 value 属性指定选中状态下对应的值,并经过 ng-model 将单选框与 $scope 中的属性对应,便实现了 type=”radio” 时的双向动态绑定。htm
2. <input type="checkbox" /> :input
<input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="phone.play_sound" />铃声 <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="phone.play_vibrate" />震动 <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="phone.play_lights" />呼吸灯
经过AngularJS 的内置指令 ng-true-value 和 ng-false-value ,指定多选框在选中和未选中状态下对应的值,再经过ng-model 将其与 $scope 中的属性对应,便实现了type=”checkbox” 的双向动态绑定。it
注:若是不使用 ng-true-value 和 ng-false-value ,则默认选中状态下的值为 "true" ,未选中状态下的值为 "false" 。io
完。class