手机版

AngularJS获取ng-repeat动态生成的ng-model值示例的详细说明

时间:2021-09-11 来源:互联网 编辑:宝哥软件园 浏览:

AngularJS获取ng-repeat动态生成的ng-模型值

最近项目遇到ng-model是由ng-repeat动态生成的,ng-model=“variable”,什么变量是未知的,所以在$scope中得不到值。“变量”,即使你得到一个值,它也是值之一。对于这样的问题,百度经过一番搜索已经找到了解决办法,可以记录在这里,帮助大家。

密码

超文本标记语言

Div Div class=' modal-header ' H3 class=' modal-title '用例集全局参数配置/H3/Div class=' modal-body ' table class=' table table-hover '第一个参数/第一个参数值/第一个/第一个/第一个/第二个-重复=' params in params ' tr TD { { param } }/TD tdinput name=' test ' class=' form-control ' type=' text ' ng-trim=' false ' ng-model=' $ parent . conf[$ index]'//

var modalinstecrel=function($ scope,$modalInstance,params){ $ scope . params=params;$ scope . conf=[];$ scope . ok=function(){ console . log($ scope . conf);$ modalinstance . close($ scope . conf);};$ scope . cancel=function(){ $ modalinstance . mission(' cancel ');};};问题描述

因为ng-model是由ng-repeat动态生成的,所以ng-model=“variable”,这个变量是未知的,所以不能在$scope中得到值。“变量”,即使你得到一个值,它也是值之一。这个问题困扰了我一天,终于解决了。

解决办法

首先,ng-model设置为$parent.conf[$index]:

使用$parent的原因是ng-repeat,它为每个输入生成一个子作用域对象,$parent意味着使用父类的作用域,这样我们就可以得到JS文件中的值。$index代表ng-repeat=' params中的param '。遍历时的下标conf是我们的变量名在js中的实际效果。我们在控制器中定义了一个$scope.conf=[]。它只是一个数组,只是通过上面的代码,元素被添加到数组中,然后我们只是通过scope.conf自动保存ng-model的所有元素

实际效果:

感谢您的阅读,希望对大家有所帮助,感谢您对本网站的支持!

版权声明:AngularJS获取ng-repeat动态生成的ng-model值示例的详细说明是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。