AngularJS中获取ng-repeat动态生成的ng-model值
来源:互联网 发布:mysql数据库教程pdf 编辑:程序博客网 时间:2024/05/18 07:08
angularJS动态设置model,并设置/获取model的值
代码
html
<div> <div class="modal-header"> <h3 class="modal-title">用例集全局参数配置</h3> </div> <div class="modal-body"> <table class="table table-hover"> <thead> <tr> <th>参数</th> <th>参数值</th> </tr> </thead> <tbody ng-repeat="param in params"> <tr> <td>{{param}}</td> <td><input name="test" class="form-control" type="text" ng-trim="false" ng-model="$parent.conf[$index]"/></td> </tr> </tbody> </table> </div> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()"> 应用 </button> <button class="btn btn-warning" ng-click="cancel()">取消</button> </div></div>
JS
var ModalInstanceCtrl = function ($scope, $modalInstance, params) { $scope.params = params; $scope.conf = []; $scope.ok = function () { console.log($scope.conf); $modalInstance.close($scope.conf); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; };
问题描述
因为ng-model
是ng-repeat
动态生成的,ng-model
=”变量”,什么变量,是未知的,所以你无法在$scope."变量"
取到值,就算取到值也是其中一个值,这个问题困扰了我一天,终于解决了。
解决方法
首先ng-model
设置为$parent.conf[$index]
:
- 用
$parent
的原因是ng-repeat
产生的,他会为每一个input
生成一个子scope
对象,而$parent
表示用父类的scope
,这样我们在JS
文件中才能取到该值。 $index
代表的意思是ng-repeat="param in params"
遍历时的下标conf
是我们在js
中的变量名
我们在controller
中定义了一个$scope.conf = [];
就是一个数组,刚好通过上面的代码,为该数组添加了元素,然后我们通过scope.conf
刚好把ng-model
的所有元素自动保存了。
实际效果
3 1
- AngularJS中获取ng-repeat动态生成的ng-model值
- AngularJS中获取ng-repeat动态生成的ng-model值
- AngularJS中获取ng-repeat动态生成的ng-model值
- angularJs中ng-repeat遍历动态生成的表单元素绑定ng-model无效的问题
- AngularJS中ng-app、ng-model、ng-bind、ng-init、ng-repeat、自定义指令
- Angularjs中select 的ng-repeat 和 ng-options 用法和获取选取值
- Angularjs中select 的ng-repeat 和 ng-options 用法和获取选取值
- AngularJs 在ng-repeat中动态使用ng-model进行双向数据绑定
- AngularJs 在ng-repeat中动态使用ng-model进行双向数据绑定(二)
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-repeat下使用ng-model
- AngularJS ng-if ng-repeat下使用ng-model
- shiro + mybatis+ spring (只用shiro的密码校验和并发剔除)——不用权限之类
- 奋斗吧,程序员——第四十章 一面风情深有韵,半笺娇恨寄幽怀
- 文章标题
- Mysql学习总结(18)——Mysql主从架构的复制原理及配置详解
- 有用的技术大神网址
- AngularJS中获取ng-repeat动态生成的ng-model值
- 2进制
- 欢迎使用CSDN-markdown编辑器
- 事件响应链,点击屏幕时是如何互动的,事件的传递
- 16进制
- SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
- 大数据量时Mysql的优化要点
- Nginx 关于 Rewrite 执行顺序详解
- Visual Studio Express: 实现同一套代码针对不同平台工程的编辑和编译