以角度获取所选选项的值

时间:2015-04-08 14:49:29

标签: angularjs

我想获得city内所选{{cities}}的价值(例如孟买/浦那)。这是iv'e到目前为止所尝试的:

HTML

<select ng-model="cities" ng-options="store as store for (store,city) in stores">
    <option value="" ng-selected="index">select city</option>
</select>

<div>
  {{cities}}    
</div>

JS

$scope.stores = {
    "Mumbai": [
        {
            area: "Kurla",
            storeName: "Name 2",
            address: "Adresrs",
            phone1: "2345",
            phone2: "345235"
        },
        {
            area: "Kalyan",
            storeName: "Name @3",
            address: "Aressf",
            phone1: "2352345",
            phone2: ""
        },
        {
            area: "Kharghar",
            storeName: "Test 1",
            address: "wert",
            phone1: "3456345",
            phone2: ""
        }
    ],
    "Pune": [
        {
            area: "test",
            storeName: "Name 2",
            address: "Address 2",
            phone1: "234423423",
            phone2: "234234b"
        },
        {
            area: "test2",
            storeName: "Name 1",
            address: "test 4 address",
            phone1: "8108156730",
            phone2: ""
        },
        {
            area: "test3",
            storeName: "test Address",
            phone1: "4234234",
            phone2: ""
        }
    ]
}

demo http://codepen.io/anon/pen/WbBBrw

2 个答案:

答案 0 :(得分:1)

更好的ng-options语法,value as text for (key, val) in object

ng-options="store as store for (store,city) in stores"

http://codepen.io/anon/pen/LEooda

答案 1 :(得分:0)

尝试以下方法:

<select ng-model="cities" ng-options="store as store for (store,city) in stores">...