简体   繁体   中英

Set key value in ngOptions AngularJS

I am trying to create a select dropdown using ngOptions. How would I go about listing the city names as options in a select from the data below?

So the options would be:

<option>Perugia</option>
<option>New Brunswick</option>
<option>Vicenza</option>


{
    Perugia: {
        country: [
            "IT"
         ]
    },
    New Brunswick: {
        state: [
            "NJ"
        ]
    },
    Vicenza: {
        country: [
            "IT"
        ]
    }
}

I created a fiddle, check it

https://jsbin.com/jarepo/edit?html,js,output

<select name="mySelect" id="mySelect"
  ng-options="name as name for (name, obj) in cities"
  ng-model="selected">

JS,

$scope.cities = {
  Perugia: {
    country: [
      "IT"
    ]
  },
  'New Brunswick': {
    state: [
      "NJ"
    ]
  },
  Vicenza: {
    country: [
      "IT"
    ]
  }
};
$scope.selected = "Perugia";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM