欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS 單選框及多選框的雙向動態(tài)綁定

 更新時間:2017年04月20日 09:49:45   作者:孫華強(qiáng)  
本篇文章主要介紹了AngularJS 單選框及多選框的雙向動態(tài)綁定的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧

AngularJS 在 <input type="text" /> 中實(shí)現(xiàn)雙向動態(tài)綁定十分簡單,如下所示:

<input type="text" ng-model="topic.title" />

只需要用ng-model 與 $scope 中的屬性對應(yīng),即實(shí)現(xiàn)了type=”text” 的雙向動態(tài)綁定。當(dāng) <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" />回學(xué)校 

通過 value 屬性指定選中狀態(tài)下對應(yīng)的值,并通過 ng-model 將單選框與 $scope 中的屬性對應(yīng),便實(shí)現(xiàn)了 type=”radio” 時的雙向動態(tài)綁定。

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 的內(nèi)置指令 ng-true-value 和 ng-false-value ,指定多選框在選中和未選中狀態(tài)下對應(yīng)的值,再通過ng-model 將其與 $scope 中的屬性對應(yīng),便實(shí)現(xiàn)了type=”checkbox” 的雙向動態(tài)綁定。

但是理想跟現(xiàn)實(shí)總是相差太多,在實(shí)際操作過程中還是出現(xiàn)了問題。應(yīng)該是ng-repeat中scope作用域的問題。

經(jīng)過一番搜索、調(diào)試,自己終于將此問題解決了,效果圖如下:

核心源碼

js

var str = ""; // 原來存放選中的項(xiàng) 
$scope.Selected = false; //默認(rèn)未選中 
var choseArr=[]; // 定義數(shù)組用于存放前端顯示 
$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; // 前端顯示所選數(shù)量 
 $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> 

參考文獻(xiàn):

http://www.dbjr.com.cn/article/68155.htm

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論