1

I'm getting an error saying that I reached 10 $digest iterations. 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.

So, what this does is iterate over a collection named triggers. There are different types of triggers, and the properties of these triggers are returned by a function on the scope called `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.

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

Community
  • 1
  • 1
bigblind
  • 12,539
  • 14
  • 68
  • 123

1 Answers1

1

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

The name property would probably be a good choice to use as a hash key.

jpsimons
  • 27,382
  • 3
  • 35
  • 45
  • getTriggerProps returns a list of objects, which I'm iterating over. the object.name is correctly displaying, so angular is correctly iterating over the list of property objects. It's only invocating this function once to get the list, to then iterate over that. – bigblind Jun 28 '13 at 06:44
  • I've added a link to the page I'm working on, in case I've scrapped something important, hoping to clean up the markup. – bigblind Jun 28 '13 at 06:52