Angularjs ng-model 在 ng-repeat 中通过 (key,val) 不更新 [英] Angularjs ng-model inside ng-repeat via (key,val) not updating

查看:25
本文介绍了Angularjs ng-model 在 ng-repeat 中通过 (key,val) 不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我不工作的演示

<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.

Working demo

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屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆