gpt4 book ai didi

javascript - 带有 key 对对象数组的 ng-options

转载 作者:行者123 更新时间:2023-12-02 17:29:10 25 4
gpt4 key购买 nike

我从第三方获得了以下数据:

countries: [
{"US":"United States"},
{"CA":"Canada"},
{"AF":"Afghanistan"},
{"AL":"Albania"},
{"DZ":"Algeria"},
{"DS":"American Samoa"},
.....
];

数据的组织方式不是我想要的,但现在我需要将所有这些都放入带有 ng-options 的选择中,以便显示的值和选项都是国家/地区的全名

我尝试过 ng-options="code as name for (code, name) in states" 但我得到一个对象作为我的选择值。

最佳答案

在这里工作:http://plnkr.co/edit/nbWHz8g4Gn0vGuSHM5o0?p=preview

尽管正如尼古拉斯指出的那样,转换数据可能更好?

HTML:

<!DOCTYPE html>
<html ng-app="app">

<head>
<script data-require="angular.js@1.2.14" data-semver="1.2.14" src="http://code.angularjs.org/1.2.14/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body ng-controller="AppCtrl">

<select id="countries" name="countries" ng-model="selectedCountry"
ng-options="getKey(country) as country[getKey(country)] for country in countries"></select>

Selected country: {{selectedCountry}}
</body>

</html>

JS:

// Code goes here

angular.module('app',[]).controller("AppCtrl", ["$scope", function($scope) {
$scope.countries = [
{"US":"United States"},
{"CA":"Canada"},
{"AF":"Afghanistan"},
{"AL":"Albania"},
{"DZ":"Algeria"},
{"DS":"American Samoa"}
];

$scope.getKey = function(country) {
return Object.keys(country)[0];
}
}]);

关于javascript - 带有 key 对对象数组的 ng-options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22490621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com