AngularJS 单选框及多选框的双向动态绑定
网络编程 2021-07-04 18:31www.168986.cn编程入门
本篇文章主要介绍了AngularJS 单选框及多选框的双向动态绑定的相关知识。具有很好的参考价值。狼蚁网站SEO优化跟着长沙网络推广一起来看下吧
AngularJS 在 <input type="text" /> 中实现双向动态绑定十分简单,如下所示:
<input type="text" ng-model="ic.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.medt}}</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>
参考文献:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持狼蚁SEO!
上一篇:Vue.js实现模拟微信朋友圈开发demo
下一篇:Vue如何引入远程JS文件
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程