Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Default value with ng-options not working with same object values #10255

Closed
natcohen opened this issue Nov 28, 2014 · 5 comments
Closed

Default value with ng-options not working with same object values #10255

natcohen opened this issue Nov 28, 2014 · 5 comments

Comments

@natcohen
Copy link

Hello,

I have the following array (please note that the name and the code are the same...if they are not the same there is no issue):

$scope.currencies = [{code:'USD', name:'USD'}, {code:'EUR', name:'EUR'}];

When I want to put it in a select:

<select ng-model='myCurrency' ng-options='currency.code as currency.name for currency in currencies'>

I get the list I want but if I allocate a default value:

$scope.myCurrency = 'USD';

I don't have any pre-selection.

It seems to work on earlier versions (1.2...) I'm using angularjs 1.3.4.

Happy thanksgiving!

@gkalpak
Copy link
Member

gkalpak commented Nov 28, 2014

It seems to work as expected for me.
It's probably something else that causes the error.

@pkozlowski-opensource
Copy link
Member

@gkalpak thnx for the reproduce, it looks completely valid & OK to me.
@natcohen please provide reproduce scenario, otherwise we can't help you.

@thomashilzendegen
Copy link

Works as expected when the data is already on the scope.

But together with e.g. a $http request (or simulated with a $timeout in my fiddle), ng-options is broken since 1.3.0 when the data is written later on the scope.

http://jsfiddle.net/pr0d5j18/

A possible workaround for me is wrapping the select into a ng-if until the data is available.

@gkalpak
Copy link
Member

gkalpak commented Nov 28, 2014

Aha !

Some remarks:

  1. You said "if they are not the same there is no issue". This doesn't work for me (i.e. having different code/name still doesn't work asynchronously).
  2. This is a duplicate of Regression using ngOptions asynchronously in angular 1.3 #9714
  3. fix(select): allow ngOptions to be set in $timeout #10203 and Fix select getting confused when removing options, causing empty option to show up when not needed #10241 are supossed to fix the issue (haven't tested myself).

@natcohen
Copy link
Author

OK, you're right, it's async (sorry, I forgot to mention it)... but this is very very strange because when I have different code and names, it works!

Well I guess I have to wait for the next release.

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants