我有一个非常简单的例子,它在jsfiddle中重现了这个问题.
http://jsfiddle.net/rgaskill/Aksec/15/
<div ng-app="miniapp"> <div ng-controller="Matrix"> <h1>Enter a value in the fist cell.</h1> <table> <thead> <tr> <th>Row Name</th> <th>0</th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> </tr> </thead> <tbody> <tr ng-repeat="(row,values) in valueMap"> <td>{{row}}</td> <td ng-repeat="(col,val) in values" ><input type="text" ng-model="valueMap[row][col]"></input></td> </tr> </tbody> </table> </div> </div>
var app = angular.module('miniapp',[]); function Matrix($scope) { $scope.valueMap = { aRow: { '0': '','1': '','2': '','3': '','4': '','5': '' } }; }
是什么导致了这种奇怪的行为?
https://groups.google.com/forum/#!topic/angular/VD77QR1J6uQ/discussion
when the ngRepeat unrolls it copies the primitive into a local scope under item. Then ng-model binds to it. When you update the model you are updating a copy,not the original. but when you update the original it couses the repeater to realized that something changed in the items array and it then recreates the ng-model,which means ti re-copies it over to items. Hence the strange behavior but it is expected.
Short answer: never iterate and input bind to primitives in ngRepeat,as you are making a copy of value rather the reference and any updates are written to local scope rather then the original location.
我更新了现在有效的小提琴
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。