Supporting duplicates in ngRepeat again

1,535 views
Skip to first unread message

Raimund Merkert

unread,
Apr 16, 2013, 4:10:06 PM4/16/13
to ang...@googlegroups.com
I'm wondering what people are thinking about supporting duplicates in ngRepeat again.
Until 1.1.3 we could have duplicates like ng-repeat='i in [ 1,1,1,1,1]' and it seemed to do the right thing.

It kind of bugs me that we can't have a simple list of strings or numbers anymore.

Is there a real technical issue with supporting duplicates? Was is it a performance issue that lead to the removal of this feature?
Is this just a transient thing and may get resurrected in 1.1.5?

I've looked at the code and have an idea for how it could be supported, but before I try to create a patch, I'd like to know if there is interest
in this or whether its provably not feasible to support them anymore.

Alan Hampson

unread,
Apr 17, 2013, 3:44:51 PM4/17/13
to ang...@googlegroups.com
I might be missing the point here, but it does work with duplicates in 1.1.4

Could you elaborate a little more on the conditions in which it doesn't work for you? :)

I've made an example with 1.1.4 where it does work

http://plnkr.co/edit/GJq3yEpiD5jQNPfDFbD9

Raimund Merkert

unread,
Apr 17, 2013, 8:31:42 PM4/17/13
to ang...@googlegroups.com
I looked at your example and for a moment I thought I was crazy because it worked. 

Then I realized what was going on. Basically, the code that checks for duplicates is only triggered on updates to the list. So, you can have 
an initial list of [1,1,1,1], but you can't ever add anything to remove anything from it.

Here's a fiddle to illustrate the problem: http://jsfiddle.net/spamdaemon/uH6Zy/  (add another item 1 or initialize the list with [1,1,1] and then add something else) and check the console

You'll see this: 
Error: Duplicates in a repeater are not allowed. Repeater: i in list at ngRepeatAction (http://code.angularjs.org/1.1.4/angular.js:14891:20) at Object.$watchCollectionAction [as fn] (http://code.angularjs.org/1.1.4/angular.js:8476:11) at Object.Scope.$digest (http://code.angularjs.org/1.1.4/angular.js:8570:27) at Object.Scope.$apply (http://code.angularjs.org/1.1.4/angular.js:8771:24) at HTMLButtonElement.<anonymous> (http://code.angularjs.org/1.1.4/angular.js:13970:17) at HTMLButtonElement.b.event.dispatch (http://code.jquery.com/jquery-1.9.1.min.js:3:28337) at HTMLButtonElement.v.handle (http://code.jquery.com/jquery-1.9.1.min.js:3:25042)

This isn't an unknown problem and is mentioned in the release notes for 1.1.4; this was first issue that hit me after updating to 1.1.4.

Michiel Kikkert

unread,
Apr 24, 2013, 6:07:17 AM4/24/13
to ang...@googlegroups.com
I had a similar problem with the same Duplicate error (I'm expecting that the root cause might be the same), however, I was encountering this when trying to add additional objects to my model array whereby the object needed a certain template. It seems (logical) that Javascript added this by Reference to my model and therefore, it triggered the duplicate error.

The way I got around this was by actuallt creating a new (empty) object in my factory that I extended with the template using angular.extend. This way, event though the resulting object is identical, it's no longer by reference and it DID allow me to add these 'duplicate' objects (well.. objects that looked the same) to my model.

I looked at your Fiddle and though I could apply the same principle, however, because of the primitive string (item), this didn't seem possible. I actually did find a way to get the correct behaviour, however, it involves extending your item string into an (new) object so this might not be a correct solution. Does seem to do the job though :-)

http://jsfiddle.net/MikeOne/sCsEG/

Raimund Merkert

unread,
Apr 24, 2013, 9:18:48 AM4/24/13
to ang...@googlegroups.com
Of course, I ended up doing something similar (I had to contend with bug 1875), but I don't really like it.
The one thing I really like about angular is that I can use MY model and don't have to adapt my model to work with angular.
That's how it should be in this case, too.



--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/Biy9jjsyHNM/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Raimund Merkert

unread,
Apr 25, 2013, 11:52:10 AM4/25/13
to ang...@googlegroups.com
I was just shown how to support duplicates in ngRepeat. I did not see it that  in the docs.

Use ng-repeat='i in [1,1,1] track by $index'

Message has been deleted

Martín Coll

unread,
May 12, 2013, 6:56:29 PM5/12/13
to ang...@googlegroups.com
You saved my day.

Thank you, Raimund.

joshkurz

unread,
Jun 7, 2013, 8:21:16 AM6/7/13
to ang...@googlegroups.com
+1
Reply all
Reply to author
Forward
0 new messages