如果嵌套的json数据有角度js,如何设置ng-options的默认值?

时间:2017-02-23 05:15:14

标签: javascript angularjs json nested angularjs-ng-options

我想通过使用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'] },
    }
}

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

3 个答案:

答案 0 :(得分:1)

您可以像这样使用 ng-init 来设置 ng-option 的默认值。

   <select id="state" 
       ng-disabled="!statessource"
       ng-model="citiessource"
       ng-init=" citiessource = countries[0]"  
       ng-options="state for (state,city) in statessource"
       ng-change="GetSelectedState()">

    </select>

答案 1 :(得分:0)

您可以通过修改citiessource变量来设置默认值。因此,要将默认值设置为USA,您需要$scope.citiessoruce = 'USA'

答案 2 :(得分:0)

ng-model设置为您要在页面加载时指定的值。

$scope,citiessource = $scope.countries;