简体   繁体   English

数据绑定不适用于嵌套ng-repeat

[英]data-binding not working on nested ng-repeat

When nesting ng-repeat it seems that clicking on the toggle button updates that addon in EVERY 'pair' (the first ng-repeat). 嵌套ng-repeat ,似乎单击切换按钮会更新每个“对”(第一个ng-repeat)中的插件。 Can anyone explain to me why this is the case and what I can do to fix it? 谁能向我解释为什么会这样,我该如何解决? Check the link above for the custom directive code... 检查上面的链接以获取自定义指令代码...

<div ng-repeat="pair in pairs track by $index">
    <h3>Pair {{ $index + 1}}</h3>

    <div class='fieldrow'>
    <button ng-repeat="addon in addons track by $index" toggle-button="addon.added">{{addon.name}} (£{{addon.price}})</button>
    </div>
</div>

What is pair and what is addon, and how are they related. 什么是配对,什么是插件,以及它们之间的关系。 Right now, even though you have a nested ng-repeat, you have completely independent data structures, so addons array is same for each pair. 现在,即使您有嵌套的ng-repeat,您也具有完全独立的数据结构,因此每对插件数组都是相同的。 Trott. 特罗特

Based on this insight, I changed ng-repeat="addon in addons track by $index" to ng-repeat="addon in pair.addons track by $index" 基于此见解,我将ng-repeat="addon in addons track by $index" ng-repeat="addon in pair.addons track by $index"

Problem solved, thanks! 问题解决了,谢谢!

I think your code does not have any problem. 我认为您的代码没有任何问题。 see plunker here. 在这里看到unk this code is working well: 该代码运行良好:

<div ng-repeat="pair in pairs track by $index">
    <h3>Pair {{ $index + 1}}</h3>

    <div class='fieldrow'>
    <button ng-repeat="addon in addons track by $index" toggle-button="addon.added">{{addon.name}} (£{{addon.price}})</button>
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM