简体   繁体   English

在angulajrs中迭代时编辑变量

[英]editing variable while iterating over it in angulajrs

I'm getting an error saying that I reached 10 $digest iterations. 我收到一个错误消息,说我达到了10次$ digest迭代。 Other questions like this one , tell me that that is because I'm changing a variable while iterating over it. 这样的其他问题,告诉我那是因为我在迭代变量时更改了变量。 The problem is that I'm making an interface in which that would be really useful to do. 问题是我正在创建一个界面,在该界面中确实很有用。 My code currently looks like this. 我的代码当前看起来像这样。

<div ng-repeat="(index, trigger) in triggers" ng-click="select(index)">
    <strong ng-bind="trigger.name></strong>
    <div ng-show="selected == index" class="span12">
        <h5>edit properties</h5>
        <div ng-repeat="property in getTriggerProps(trigger.type) ">
        <div ng-bind="property.name"></div>
        <div ng-switch on="property.name">
            <input type="text" ng-switch-when="'text'" ng-model="trigger[property.name]">
            <select type="select" ng-switch-when="'select'" ng-model="trigger[property.name]" ng-options="possible for possible in property.options">
            </select>
        </div>
    </div>              </div>
</div>

In my real code, there are a lot more twitter bootstrap related divs and classes, but I've left them out for clarity. 在我的真实代码中,还有更多与Twitter Bootstrap相关的div和类,但为清晰起见,我将其省略。

So, what this does is iterate over a collection named triggers. 因此,这是对名为triggers的集合进行迭代。 There are different types of triggers, and the properties of these triggers are returned by a function on the scope called `getTriggerProps. 触发器有不同的类型,这些触发器的属性由范围为`getTriggerProps的函数返回。

For the list of returned properties, I create a series of inputs based on the property type. 对于返回的属性列表,我根据属性类型创建了一系列输入。 The model of the property, will be the actual property on the trigger object. 属性的模型将是触发器对象上的实际属性。 But that's the problem I think I'm having, I'm changing properties of the trigger while iterating over the collection this trigger is in. 但这就是我想遇到的问题,我在遍历此触发器所在的集合的同时更改了触发器的属性。

I'm using angularjs 1.1.5, which I'm aware is an unstable version, but I wanted to play with its features. 我使用的是angularjs 1.1.5,我知道它是一个不稳定的版本,但我想使用它的功能。

In case seeing the entire page I'm working on could help, here's a public link: https://dl.dropboxusercontent.com/u/12041395/neurobot_interface_proposal.html 如果看到我正在处理的整个页面可能会有所帮助,请使用以下公共链接: https : //dl.dropboxusercontent.com/u/12041395/neurobot_interface_proposal.html

So, changing the objects your iterating on isn't a problem. 因此,更改迭代对象并不成问题。 The way ng-repeat works is, it tags the objects with a hashKey that's like a unique ID, so it knows they're the same instance. ng-repeat的工作方式是,它使用类似于唯一ID的hashKey标记对象,因此它知道它们是同一实例。 Just adding properties wouldn't break that. 仅添加属性不会破坏它。

I think your issue may be that you're iterating over newly created objects? 我认为您的问题可能是您要遍历新创建的对象? Not sure what getTriggerProps does, but if it creates new objects every invocation, that will break for sure. 不知道getTriggerProps做什么,但是如果每次调用都会创建新对象,那肯定会中断。 If that's the case, the solution is to add your own hashKey property to each one, which will be the same every time if the object is conceptually the same. 如果是这样,解决方案是向每个对象添加自己的hashKey属性,如果对象在概念上相同,则每次都将相同。

The name property would probably be a good choice to use as a hash key. name属性可能是用作哈希键的不错选择。

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

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