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.
ngOptions mutilating ordering of values in array #9465
Do refer to this updated fiddle, and note the position of the string "011" in the select dropdown. Shouldn't it be at the bottom rather than the third position?
Ok, now I know the problem:
If you do (key, value) in years we assume that years is an object. So we get all the entries from it and sort the keys. In your case years is an array, i.e. the keys are '0','1','2','3','4','5',... (as strings, as properties of objects are always strings). As iteration over properties in objects do not have a defined order, angular sorts those keys, and as your example has 11 entries that last entry has the property 10 and is therefore put behind the property 1, which is the second entry.
To fix your problem, don't use (key, value) in years but rather year for year in years, see http://jsfiddle.net/97h62/47/.
So but yes, it is weird that using (key, value) automatically assumes an object datasource. We should actually check the type of the datasource instead of relying on the syntax that has been used.
Thanks tbosch. I did get the logic behind the sorting, and it is rather inevitable if angular is naïve enough to treat the array indices as strings. But, in the absence of properly specifying a track by expression along with select as, I feel that there is no way to go other than applying transformers before and after the selection to pass my data.
And no, the year for year in years would not work in my case, as it's actually the index my app really needs :(
Activity
tbosch commentedon Oct 9, 2014
Hi,
this is fixed in the latest 1.2.x (1.2.26) and 1.3.x (1.3.0-rc.5) release, see http://jsfiddle.net/97h62/45/
kumarharsh commentedon Oct 10, 2014
@tbosch It's not fixed.
Do refer to this updated fiddle, and note the position of the string "011" in the select dropdown. Shouldn't it be at the bottom rather than the third position?
http://jsfiddle.net/97h62/46/
tbosch commentedon Oct 10, 2014
Ok, now I know the problem:
If you do
(key, value) in yearswe assume thatyearsis an object. So we get all the entries from it and sort the keys. In your caseyearsis an array, i.e. the keys are'0','1','2','3','4','5',...(as strings, as properties of objects are always strings). As iteration over properties in objects do not have a defined order, angular sorts those keys, and as your example has 11 entries that last entry has the property10and is therefore put behind the property1, which is the second entry.To fix your problem, don't use
(key, value) in yearsbut ratheryear for year in years, see http://jsfiddle.net/97h62/47/.So but yes, it is weird that using
(key, value)automatically assumes an object datasource. We should actually check the type of the datasource instead of relying on the syntax that has been used.tbosch commentedon Oct 10, 2014
Note that this has been there since the beginning of
ngOptions, so it is no regression...kumarharsh commentedon Oct 11, 2014
Thanks tbosch. I did get the logic behind the sorting, and it is rather inevitable if angular is naïve enough to treat the array indices as strings. But, in the absence of properly specifying a
track byexpression along withselect as, I feel that there is no way to go other than applying transformers before and after the selection to pass my data.And no, the
year for year in yearswould not work in my case, as it's actually theindexmy app really needs :(petebacondarwin commentedon Jan 11, 2015
This is fixed by #10639 - see http://jsfiddle.net/8gLagt22/ - ngOptions now doesn't attempt to sort the keys of an object at all.
Narretz commentedon Sep 13, 2015
This is fixed in 1.4. If you are still on 1.3.x, you are encouraged to upgrade. Compared to 1.2 -> 1.3 the upgrade is quite painless.:)