AngularJS中获取ng-repeat动态生成的ng-model值
2017-09-14 11:05
465 查看
angularJS动态设置model,并设置/获取model的值
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
因为
首先
用
我们在
代码
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>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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'); }; };1
2
3
4
5
6
7
8
9
10
11
12
问题描述
因为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的所有元素自动保存了。
实际效果
相关文章推荐
- AngularJS中获取ng-repeat动态生成的ng-model值
- AngularJS中获取ng-repeat动态生成的ng-model值
- AngularJS 获取ng-repeat动态生成的ng-model值实例详解
- angularJS指令ng-repeat生成的dom元素js获取不到
- angularJs中ng-repeat遍历动态生成的表单元素绑定ng-model无效的问题
- 如何在angularJS深层嵌套ngRepeat中获取不同层级的$index
- AngularJs中 ng-repeat指令中实现含有自定义指令的动态html的方法
- angularJS动态生成的页面中,ng-click无效解决办法
- Angularjs中select 的ng-repeat 和 ng-options 用法和获取选取值
- 理解AngularJS生命周期:利用ng-repeat动态解析自定义directive
- angularJS动态生成的页面中,ng-click无效解决办法
- AngularJs 在ng-repeat中动态使用ng-model进行双向数据绑定
- AngularJS获取ng-repeat中的单选框
- AngularJs 在ng-repeat中动态使用ng-model进行双向数据绑定(二)
- angularjs 通过click获取ngrepeat出来的值
- angularjs(ng-repeat和filter)生成多级菜单
- 详解angularJS动态生成的页面中ng-click无效解决办法
- Angularjs中select 的ng-repeat 和 ng-options 用法和获取选取值
- AngularJs ng-repeat 嵌套如何获取外层$index
- angularJS动态设置ng-model