ng-options Clearing - Duplicates in ng-repeat, ng-option - Angular 1.1.5

906 views
Skip to first unread message

Keith Rosenberg

unread,
Aug 8, 2013, 7:31:30 PM8/8/13
to ang...@googlegroups.com
I have created a Plunkr to illustrate a problem I am having with a ng-option nested in an ng-repeat: http://plnkr.co/edit/km4uoNPRjh624QHgEeeB?p=preview

If you "add stuff" or "add things" and then you go ahead and select it, and then add another one, it automagically removes your last selected element. I cannot quite grok whether this is a failure of my own, or a failure of the Angular library to support duplicates (I know the functionality has been removed, but I'm saying maybe it shouldn't have been).

Can anybody direct me toward a solution? The 1.1.5 docs for ng-repeat do not have a good example of using the track by feature to prevent the duplicates error I get by emitting it: http://code.angularjs.org/1.1.5/docs/api/ng.directive:ngRepeat

There is a talk on the issue here at https://github.com/angular/angular.js/pull/2505 and I have also checked through a few posts in the group as well where people had this problem, many of whom seemed to find a solve by using the track by expression - I however, think I have either not understood how to implement this, or Angular has a flaw/bug I should report.

Thanks in advance!

Keith Rosenberg

unread,
Aug 8, 2013, 9:09:14 PM8/8/13
to ang...@googlegroups.com
It looks possible that there may have been a scope/closure issue with the the "stuff" array where, for the ng-options, select, you need to set your model not to the key of the variable you are looping through, but instead, reference it from the higher level property you are using in your loop.

AKA if you are saying, "for item in parent.items, bind to item" - that does not work, instead you need to say, "for item in parent.items, bind to parent.items[$index]" - this seems a bit janky, but does look like it solves the issue: http://plnkr.co/edit/1CmulrjigYQYjmfewUcJ?p=preview
Reply all
Reply to author
Forward
0 new messages