使用带有2个数据模型的ng-options

时间:2014-05-20 19:12:12

标签: angularjs

我已经选择使用ng-options填充选项:

<select ng-model="selectedState" name="state_select" id="state_select" 
        style="float:left;" ng-options="item.State for item in Info | orderBy: 'State'"
        ng-change="stateSelected(this)">
    <option value="">Please select a State</option>
</select>

目前,它在选项的值和文本中填充了州缩写。我需要将状态NAMES放在文本中,而不是状态缩写。 状态缩写在Info.State值中出现,我有一个州名称数组:

$scope.StatesArray = {"AL": "Alabama","AK": "Alaska","AZ": "Arizona","AR": "Arkansas","CA": "California","CO": "Colorado","CT": "Connecticut",
                      "DE": "Delaware","DC": "District of Columbia","FL": "Florida","GA": "Georgia","HI": "Hawaii","ID": "Idaho","IL": "Illinois",
                      "IN": "Indiana","IA": "Iowa","KS": "Kansas","KY": "Kentucky","LA": "Louisiana","ME": "Maine","MD": "Maryland","MA": "Massachusetts",
                      "MI": "Michigan","MN": "Minnesota","MS": "Mississippi","MO": "Missouri","MT": "Montana","NE": "Nebraska","NV": "Nevada",
                      "NH": "New Hampshire","NJ": "New Jersey","NM": "New Mexico","NY": "New York","NC": "North Carolina","ND": "North Dakota",
                      "OH": "Ohio","OK": "Oklahoma","OR": "Oregon","PA": "Pennsylvania","PR": "Puerto Rico","RI": "Rhode Island","SC": "South Carolina",
                      "SD": "South Dakota","TN": "Tennessee","TX": "Texas","UT": "Utah","VT": "Vermont","VA": "Virginia","WA": "Washington",
                      "WV": "West Virginia","WI": "Wisconsin","WY": "Wyoming"};

如何使<option>的VALUE成为州名缩写,TEXT成为州名,如下所示:?

<option value="AL">Alabama</option>

1 个答案:

答案 0 :(得分:1)

你可能会这样做(未经测试)

ng-options="item.State as StatesArray[item.State] for item in Info

或者因为你有一个同时具有appreviation和全名的对象:

ng-options="key as state for (key, state) in StateArray"