gpt4 book ai didi

javascript - 在嵌套的 json 数据 Angular js 的情况下,如何为 ng-options 设置默认值?

转载 作者:行者123 更新时间:2023-11-30 11:41:44 25 4
gpt4 key购买 nike

我想通过使用 ng-init、ng-model 等为我的 ng-options 设置一个默认值。我正在使用可靠的下拉菜单。我的代码如下:

 <select id="country" ng-model="statessource" ng-disabled="!type2"  ng-options="country for (country, states) in countries" ng-change="GetSelectedCountry()">
<option value='' selected>Select Country</option>
</select>

<select id="state" ng-disabled="!statessource" ng-model="citiessource" ng-options="state for (state,city) in statessource"
ng-change="GetSelectedState()" >
<option value=''>Select State</option>
</select>

像这样的一些数据:

$scope.countries = {
'USA': {
'IL': { 'CHICAGO': ['60633', '60646', '60653', '60656', '60607', '60632', '60643', '60652', '60620', '60612', '60661', '60624', '60634', '60601', '60827', '60644', '60631', '60630'] },
}
}

我想在国家/地区下拉列表中设置默认值美国。我应该怎么做。请给我建议。

最佳答案

根据您发布的代码,您尚未在此处提供完整代码,您的代码中有太多错误。

定义另一个 $scope 变量,它选择了名为“USA”的默认对象。您可以按照以下方式执行此操作:

$scope.countries = {
'USA': {
'IL': {
'CHICAGO': ['60633', '60646', '60653', '60656', '60607', '60632', '60643', '60652', '60620', '60612', '60661', '60624', '60634', '60601', '60827', '60644', '60631', '60630']
},
}
}
$scope.selected = $scope.countries["USA"]; // this needs to be added

您的 View 部分将如下所示:

 <select id="state" ng-disabled="!countries" ng-model="selected" 
ng-options="state for (state,city) in countries" ng-change="GetSelectedState()">
<option value=''>Select State</option>
</select>

还有一个工作 Plunker

希望这会有所帮助。

关于javascript - 在嵌套的 json 数据 Angular js 的情况下,如何为 ng-options 设置默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42407595/

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