ng-repeat产生的ng-model中取不到值的问题
来源:互联网 发布:sd卡数据恢复安卓版 编辑:程序博客网 时间:2024/05/17 07:02
最近遇到在ng-repeat产生的textarea中绑定ng-model后,在js中取不到ng-model值的问题。html的代码结构如下
<div class="ques-item hide1 show9a" id="q10"> <div class="ques-item-question"> 10.{{questions[9].questionContent}} </div> <div class="ques-item-option"> <div ng-repeat="option in questions[9].options"> <input type="{{questions[9].questionType}}" name="problem10" value="{{option.optionCode}}" id="{{option.id}}"> <label for="{{option.id}}">{{option.optionContent}}</label> <textarea ng-if="$index == 4" class="ques-option-text" name="" id="" cols="30" rows="1" ng-model="text10"></textarea> </div> </div></div>
用ng-repeat循环输出了该题目的选项,有的选项后面有输入框,于是用ng-if控制某个选项后面添加textarea输入框。在用ng-model双向绑定了text10后,当输入框中输入内容时,js中的$scope.text10并不能取得内容。
经过查询发现原因是,ng-repeat会产生子作用域,而js中的scope是父作用域的,Angularjs中的作用域向上查找,所以是不能取得ng-repeat中的绑定值的。解决方案就是把子scope中的值通过$parent属性传递给父scope,同时把text10定义为数组,即前端绑定时使用$parent.text10[$index],这样就绑定了每一个textarea输入框的值,从而能在js中获取到。修改后如下:
<div class="ques-item hide1 show9a" id="q10"> <div class="ques-item-question"> 10.{{questions[9].questionContent}} </div> <div class="ques-item-option"> <div ng-repeat="option in questions[9].options"> <input type="{{questions[9].questionType}}" name="problem10" value="{{option.optionCode}}" id="{{option.id}}"> <label for="{{option.id}}">{{option.optionContent}}</label> <textarea ng-if="$index == 4" class="ques-option-text" name="" id="" cols="30" rows="1" ng-model="$parent.text10[4]"></textarea> </div> </div></div>
0 0
- 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对象的问题
- ionic ng-model controller中取不到值(input)
- ionic ng-model controller中取不到值(input)
- angularJS中ng-ng-repeat里的单选框(radio)和多选框(checkbox)赋值取值
- ng-repeat的使用
- 如何在ng-if中取得ng-model的值
- 解决AngularJS中ng-repeat不更新视图的问题
- AngularJS中ng-app、ng-model、ng-bind、ng-init、ng-repeat、自定义指令
- AngularJS ng-model获取不到WdatePicker值的解决方案
- 关于AngularJs的ng-repeat问题
- AngularJS中使用ng-repeat的index
- 去掉ng-repeat产生的$$hashkey和object
- Angularjs中select 的ng-repeat 和 ng-options 用法和获取选取值
- 基础命令gedit
- H/D
- Java虚拟机类加载机制
- 国际C语言乱码大赛
- 练习系统 实验四 放苹果
- ng-repeat产生的ng-model中取不到值的问题
- 程序修改文件
- G
- wireshark过滤命令
- STL总结
- css外联式和导入式的差别
- 杂文——s5pv210用户手册翻译
- Java集合类
- Exception in thread "main" java.lang.NoClassDefFoundError: entity/user (wrong name: entity/User)