AngularJS ng-repeat下使用ng-model
来源:互联网 发布:淘宝电视机19寸 编辑:程序博客网 时间:2024/06/10 09:04
AngularJS ng-repeat下使用ng-model
如图所示,想要实现点击确定修改后,得到input中的值,因此尝试用下面的代码实现:
<tr ng-repeat="item in info "> <input type="text" ng-show="aa && item.uId==index" ng-value="item.uName" ng-model="modifName"> <input type="text" ng-show="aa && item.uId==index" ng-value="item.uPwd" ng-model="modifPwd"> </tr>
通过在repeat里给每一个input绑定ng-model,当点击修改按钮后,将输入input的新值,分别用两个变量modifName、modifPwd接收.但是结果console出来都是undefined,为什么
原因:
如果,你对angularJs的$scope,有深入了解,也许这个问题就迎刃而解了。原因就在,ng-repeat、ng-switch、ng-view、ng-include都会创建一个新的子scope,所以问题经常在这些指令中出现。
进行原型继承即意味着父作用域在子作用域的原型链上,这是JavaScript的特性。AngularJS的作用域还存在如下内部定义的关系:
scope.
关于scope作用域,这篇翻译自英文的文章阐述的非常详细。 理解AngularJS的作用域Scope
解决方法::
在子作用域中使用
parent.parentScopeProperty,这样可以直接修改父作用域的属性。针对ng−repeat,因为它会对每一个item都会创建子作用域,所以使用 parent指向父作用域最好的方式。这里具体代码:<input type="text" ng-model="$parent.modifName">
遵循一个原则:避免这个问题的最佳实践是在ng-model中总使用“.“,例如:
更好:
<input type="text" ng-model="obj.modifName">
不推荐
<input type="text" ng-model="modifName">
第二个方法需要在父作用域定义obj,
$scope.obj={modifName:”“};
在父作用域中定义函数,子作用域通过原型继承调用函数把值传递给父作用域(这种方式极少使用)。
- 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
- AngularJS ng-repeat的使用
- AngularJS中ng-app、ng-model、ng-bind、ng-init、ng-repeat、自定义指令
- AngularJs 在ng-repeat中动态使用ng-model进行双向数据绑定
- AngularJs 在ng-repeat中动态使用ng-model进行双向数据绑定(二)
- angularjs select标签使用ng-repeat标签之后,ng-model无法更新问题解决
- angularjs ng-repeat
- angularjs ng-repeat
- Angularjs中的ng-repeat
- angularJS ng-repeat
- Web从设计到代码
- iOS中一些零碎的整理
- 百度2014校园招聘-研发工程师笔试题(济南站)
- BNU 33564 Chinese Dialects
- [翻译]Drools6.2帮助文档-目录
- AngularJS ng-repeat下使用ng-model
- Android Intent详解
- 操作系统功能认识
- 如何把自己常用的类封装到DLL方便使用
- 笔试1
- C++ stringstream的用法
- 第7题
- iOS学习,数据存储小结
- request属性 request.getAttribute()