AngularJS 在 <input type="text" /> 中實現雙向動態綁定十分簡單,如下所示:
<input type="text" ng-model="topic.title" />
只需要用ng-model 與 $scope 中的屬性對應,即實現了type=”text” 的雙向動態綁定。當 <input type="radio" /> 及 <input type="checkbox" /> 時情況略有不同:
1. <inputtype="radio" />
<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” 時的雙向動態綁定。
2. <input type="checkbox" />
<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” 的雙向動態綁定。
但是理想跟現實總是相差太多,在實際操作過程中還是出現了問題。應該是ng-repeat中scope作用域的問題。
經過一番搜索、調試,自己終于將此問題解決了,效果圖如下:

核心源碼
js
var str = ""; // 原來存放選中的項 $scope.Selected = false; //默認未選中 var choseArr=[]; // 定義數組用于存放前端顯示 $scope.check = function(z,x){ console.log("HUY:"); console.log(z); console.log("HUYU:"); console.log(x); if (x == false) { // 選中 str = str + z + ','; } else { str = str.replace(z + ',', ''); // 取消選中 } choseArr = (str.substr(0,str.length-1)).split(','); console.log("HY:"); console.log(choseArr); $scope.number_request = choseArr.length; // 前端顯示所選數量 $scope.content_request = choseArr; // 前端顯示所選請求ID }; Html
<tr ng-repeat="item in querydata"> <td ng-bind="$index+1">1</td> <td><a ui-sref="#">{{item.postid}}</a></td> <td>{{item.medname}}</td> <td>{{item.medfact}}</td> <td>{{item.medcnt}}</td> <td>{{item.remark}}</td> <td>{{item.tel}}</td> <td>{{item.post_time}}</td> <td><input id={{item.postid}} type="checkbox" ng-model="Selected" ng-click="check(item.postid,Selected)" /></td> </tr> 參考文獻:
//m.survivalescaperooms.com/article/68155.htm
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答