0

My array is something like this:

$scope.states = [{                  
    "AB" : "Abia",
    "AJ" : "Abuja",                
    "AN" : "Anambra"}];

Here is the HTML:

<select ng-options="state for state in states">
    <option value=""></option>
</select>

How do I properly pass it over?

6
  • Your Plunkr demo is very complicated and not representative of the question Commented May 10, 2015 at 1:51
  • @Vlad274 , just looking at the snippet of code is what i need to know m the plunker is pretty irrelevant Commented May 10, 2015 at 1:54
  • @AnthonyAkpan, what is state.[0]? this is not a valid syntax. Also, ng-options requires ng-model Commented May 10, 2015 at 1:56
  • @NewDev , just me trying to figure it out Commented May 10, 2015 at 1:58
  • @AnthonyAkpan, also your states array has only a single element. Is this intentional? And if you intend to use an object, then the syntax is ng-options="key as value for (key, value) in states" (or something like that) Commented May 10, 2015 at 1:59

2 Answers 2

3

Your states is an array with a single element - an object with states as key/value pairs. You can still use an object with ng-options - not sure though why an array is needed - so long as you refer to the source object with "... in states[0]".

ng-options also requires an ng-model.

<select ng-model="selectedState"
        ng-options="key as state for (key, state) in states[0]">
</select>

The above would set selectedState to key (i.e. the abbreviation). You can set it to state as well).

Of course, if you don't need states to be an array of a single element and just set it to the element itself, then you could refer to it as "...(key, state) in states" rather than ... in states[0]".

Sign up to request clarification or add additional context in comments.

Comments

1

JS:

$scope.states = {
        "AB" : "Abia",
        "AJ" : "Abuja",
        "AN" : "Anambra"};

HTML:

<select ng-options="k as v for (k,v) in states"></select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.