0

I'm getting a strange behavior when binding the following object to a select:

REGIONSH = {0:"All Regions",1:"Europe",2:"North America"};

HTML (where region is a integer for key):

<select ng-model="REGIONSH[region]" ng-options="name for(key,name) in REGIONSH">

The behavior is that the list is populated correctly with the correct option selected, but when I change the region variable the selected entry changes fine, but suddenly both the original and the new selections are named the same thing.

For example, I would select the second element in the list, and the original one would be renamed to "North America" from "All Regions".

The desired behavior is to have a select where I can chose from each of the values in the object while having the key as the option's value.

3
  • Also if I try setting ng-model to just the integer (which happens to also be the index) I just get an empty option with ? as the value. Commented Jun 13, 2014 at 20:38
  • 1
    I think you're using ng-model recursively, whereas it should maybe just be ng-model="region". Plunker is that what you're talking about? Commented Jun 13, 2014 at 20:47
  • @MorganDelaney The region variable is an integer that refers to a key in the object. Commented Jun 13, 2014 at 20:49

1 Answer 1

1

The way you have your html:

<select ng-model="REGIONSH[region]" ng-options="name for(key,name) in REGIONSH">

It will display the list correctly, but whenever the user makes a choice the name of the region ("name for(key,name)") will be bound to wherever ng-model is pointing to.

Meaning when the user makes a choice for say the first option, it ng-model will effectively perform

REGIONSH[region] = /*name of the selected region*/;

Which assigns a name to the region at index region.

What you probably want is ng-model="region", so the values are assigned to the region variable, and not the options. ( Or any other variable you want to store the choice in; probably not in REGIONSH though )

Then you'd also need ng-options="key as name for (key, name) in REGIONSH, so that it still displays the name, but now assigns key whenever the user makes a selection. ( Read here on the many ng-options formats you can use )

Meaning when a selection is made it performs

region = /*key of the selected region*/;

The final result would be

<select ng-model="region" ng-options="key as name for(key,name) in REGIONSH">
6
  • Hmm, I've tried this now, the thing is - when I set the region it immediately flips back to the ? option. I'm using the exact thing you have at the end of your answer. The region variable refers to a key in the object. Commented Jun 13, 2014 at 20:47
  • @ChristianStewart I've slightly modified another commenters plunkr, here's a working sample of the above code. You might need the additional option tag depending on how ancient your browser is Commented Jun 13, 2014 at 20:50
  • I don't need the initial option because the region variable will ALWAYS be a key in the object. I'm still getting the empty select option with ? as a key though. Commented Jun 13, 2014 at 20:52
  • @ChristianStewart I hope you're looking at the same thing because this is what i'm getting. Could you post the code that reproduces your problem? Commented Jun 13, 2014 at 20:55
  • Alright this is strange, I made a plunker that does the same thing as well, and it works, but in my app it doesn't. I'm streaming it here: twitch.tv/quantumdota Commented Jun 13, 2014 at 20:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.