You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
ng-options track by and select as are not compatible #6564
When both a track by expression and a select as expression are used in ng-options, binding doesn't work anymore.
For instance:
it('should bind to scope value through experession while tracking/identifying objects',function(){createSelect({'ng-model': 'selected','ng-options': 'item.id as item.name for item in values track by item.id'});scope.$apply(function(){scope.values=[{id: 10,name: 'A'},{id: 20,name: 'B'}];scope.selected=scope.values[0].id;});expect(element.val()).toEqual('10');scope.$apply(function(){scope.selected=scope.values[1].id;});expect(element.val()).toEqual('20');});
It seems that in the ngOptions directive, the track function always directly use the value expression even if a select as expression is set.
Ok, I have investigated this. It's not actually a bug, just misleading documentation.
The thing is, you just can't combine value as label for collection with track by.
You have to pick the one or the other. There are two different Use Cases:
1. You want a shiny presentation for an ugly value
Use value as label:
items = [
{value: 1, label: 'One'},
{value: 2, label: 'Two'},
]
template = 'ng-options="item.value as item.label for items"'
When you change the dropdown, the model is assigned whatever you define in front of the as.
2. You want to iterate over complex objects
Use track by:
items = [
{id: 1, ...},
{id: 2, ...},
{id: 3, ...},
]
template = 'ng-options="item as item.label for items track by items.id"'
When you change the dropdown, the model is assigned the item. The track by expression is only used to associate options with items through the value attribute on the <option>.
Thanks for bringing the issue up, and thanks for the feedback on the approaches. This is a tough problem without a clean solution that satisfies all constraints, is consistent with ng-repeat, and is backwards-compatible. In the meantime, I removed the thrown error, added tests for how track by and select as should work together, and fixed the implementation.
Unfortunately, the originally reported issue would not work with the expression/model as is, but could work with some re-working.
Activity
janv commentedon May 13, 2014
Oops, accidentally deleted my previous comment.
I created/modified a fiddle to illustrate the problem:
http://jsfiddle.net/qWzTb/418/
SQiShER commentedon May 13, 2014
+1 I'm having the same problem.
janv commentedon May 13, 2014
It's annoying but you can really just leave away the
track byand it'll work.ahoereth commentedon Jul 15, 2014
Can confirm this problem still exists as of v1.3.0-beta.15.
wojciechfornal commentedon Jul 21, 2014
Having the same problem with 1.2.20 :/
mvalim commentedon Aug 6, 2014
Same here
Failing test case for angular#6564
janv commentedon Aug 8, 2014
I've been encountering this again.
The problem was created in c32a859 by @quazzie:
For checking if an option is selected, the
trackFnis used, where thevalueFnshould be used instead. I'll try to come up with a fix.janv commentedon Aug 8, 2014
Ok, I have investigated this. It's not actually a bug, just misleading documentation.
The thing is, you just can't combine
value as label for collectionwithtrack by.You have to pick the one or the other. There are two different Use Cases:
1. You want a shiny presentation for an ugly value
Use
value as label:When you change the dropdown, the model is assigned whatever you define in front of the
as.2. You want to iterate over complex objects
Use
track by:When you change the dropdown, the model is assigned the item. The
track byexpression is only used to associate options with items through thevalueattribute on the<option>.Illustrated in http://jsfiddle.net/0vpsv1wa/1/
39 remaining items
fix(select): add basic track by and select as support
jeffbcross commentedon Oct 10, 2014
Thanks for bringing the issue up, and thanks for the feedback on the approaches. This is a tough problem without a clean solution that satisfies all constraints, is consistent with ng-repeat, and is backwards-compatible. In the meantime, I removed the thrown error, added tests for how track by and select as should work together, and fixed the implementation.
Unfortunately, the originally reported issue would not work with the expression/model as is, but could work with some re-working.
jonathansolorzn commentedon Nov 19, 2015
This is still happening in v1.4.7, no fix yet?
mariusstaicu commentedon Nov 28, 2015
same problem here!!
agvalente commentedon Dec 1, 2015
Same problem!!
DanieleSassoli commentedon Dec 8, 2015
same thing here with in v1.4.7
dlazzarino commentedon Mar 14, 2016
Same thing with v1.4.8
aj07mm commentedon May 5, 2016
Same thing with v1.5.3
hebergentilin commentedon May 10, 2016
The ng-options works fine, but when update the model manual, the select value hadn't update.
See the http://jsfiddle.net/0vpsv1wa/110/
gkalpak commentedon May 13, 2016
There are certain (documented) limitations in using
track bytogether withselect ... as.Not all usecases are supported.
mgsstms commentedon Jan 29, 2017
ng-options="item.id as item.name for item in values track by item.id" works, but doesn't se the default value
http://plnkr.co/edit/6YdeJviPnCwY3Em9ltyo?p=preview
gkalpak commentedon Jan 29, 2017
No, it doesn't work (i.e. track by has no effect).
kash-pram commentedon Jun 19, 2017
is there a solution to this yet?
https://stackoverflow.com/questions/44629489/malfunctioning-select-option-dropdowns-while-using-ngrepeat-or-ngoptions-ngmo