I was in the process of asking nearly the identical question as you when Stack Overflow suggested I look at your question. Looks like we got our Country list from the same source.
I didn't want to change the structure either. But after seeing the accepted answer, I didn't want to hack it that way. So instead I wrote a reusable service that will Morph any similar data structure.
The Service
/*
* Morphs an array of a key/val pair to have the original key, be a value, and
* give new property names
*
* @method keyValMorph
* @param {array of objects} ex: [ {US:'United States'} ]
* @param {string} keyName - desired property name for the original key
* @param {string} valName - desired property name for the original value
* @return {array of objects} ex: [ {key: 'US', val: 'United States'} ]
*/
.factory('keyValMorph', function () {
return function (data, keyName, valName) {
var sort = [], keyName = keyName || 'key', valName = valName || 'val';
for (var i = 0; i < data.length; i++) {
var obj = {};
for (var key in data[i]) {
obj[keyName] = key;
obj[valName] = data[i][key];
sort.push(obj);
}
}
return sort;
};
})
The controller call:
$scope.countriesSorted = keyValMorph($scope.countries, 'code', 'name');
It'll take your original data structure and turn it into:
$scope.countriesSorted = [
{code:"US", name:"United States"},
{code:"CA", name:"Canada"},
{code:"AF", name:"Afghanistan"},
{code:"AL", name:"Albania"},
{code:"DZ", name:"Algeria"},
{code:"DS", name:"American Samoa"}
];
HTML
<select data-ng-model="selected" data-ng-options="country.code as country.name for country in countriesSorted ">
<option value="">[-- select --]</option>
</select>