Angularjs ng-model 在 ng-repeat 中通过 (key,val) 不更新 [英] Angularjs ng-model inside ng-repeat via (key,val) not updating
问题描述
这是我不工作的演示
<div ng-repeat="(key,value) in t"><div>{{key}}</div><input type="text" ng-model="value"/>
</节>
模型保持不变.如何同步?请注意,数据结构很重要.
ng-model
绑定将在当前范围内评估其表达式.由于 ng-repeat
创建了一个子作用域,这意味着 ng-model
将在子作用域上查找名为 value
的属性.在您的示例中,我们希望它在 parent 范围内查找 val
属性,该属性的别名为 t
.
这是设计使然,在您的情况下可以通过在表达式中引用父作用域 t
来规避.
代码(注意输入元素上的绑定已更改):
<div ng-repeat="(key,value) in t"><div>{{key}}</div><input type="text" ng-model="t[key]"/>
</节>
由于您使用的是 Angular 1.3 的测试版,这可能是一个错误.
Here is my not working demo
<section ng-repeat="t in test">
<div ng-repeat="(key,value) in t">
<div>{{key}}</div>
<input type="text" ng-model="value"/>
</div>
</section>
Model stays the same. How to sync? Please note, structure of data is important.
The ng-model
binding will evaluate its expression on the current scope. As ng-repeat
creates a child scope, this means that ng-model
will look for a property named value
on the child scope. In your example we'd expect it to look for the val
property on the parent scope, which is aliased to t
.
This is by design and can be circumvented in your case by referencing the parent scope t
in the expression.
Code (notice the binding on the input element has changed):
<section ng-repeat="t in test">
<div ng-repeat="(key,value) in t">
<div>{{key}}</div>
<input type="text" ng-model="t[key]"/>
</div>
</section>
As you're using a beta version of Angular 1.3, this may be a bug.
这篇关于Angularjs ng-model 在 ng-repeat 中通过 (key,val) 不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!