【问题标题】:How to get states base on selected country in angularjs?如何在angularjs中根据所选国家/地区获取状态?
【发布时间】:2016-03-19 12:40:49
【问题描述】:

我有这样的 JSON 数据。我有两个下拉列表:一个国家和一个州。根据选择的国家/地区,我想更新州下拉菜单。

$scope.countryOptions = [
        {
            'name': 'India',
            'value': 'india',
            'states': [
                {
                    'name': 'Arunachal Pradesh',
                    'value': 'arunachal-pradesh'
                },
                {
                    'name': 'Andhra Pradesh',
                    'value': 'andhra-pradesh'
                },
                {
                    'name': 'Assam',
                    'value': 'assam'
                },
            ]
        },
        {
            'name': 'United States',
            'value': 'united-states',
            'states': [
                {
                    'name': 'New York',
                    'value': 'new-york'
                },
                {
                    'name': 'Florida',
                    'value': 'florida'
                },
                {
                    'name': 'Massechussettes',
                    'value': 'massechussettes'
                },
            ]
        },
        {
            'name': 'Australia',
            'value': 'australia',
            'states': [
                {
                    'name': 'Victoria',
                    'value': 'victoria'
                },
                {
                    'name': 'Queensland',
                    'value': 'queensland'
                },
                {
                    'name': 'New South Wales',
                    'value': 'new-south-wales'
                },
            ]
        },
    ]

【问题讨论】:

标签: javascript angularjs


【解决方案1】:
  <select ng-model="country" ng-change="update(country)" ng-options="option.value as option.name for option in countryOptions ">
     <option ng-disabled="true"   ng-selected="true" value="">Select a country</option>
  </select>
<br /> <br /> <br /> 

  <select ng-model="state" ng-options="item as item.name for item in countryOptions[index].states " >
      <option ng-disabled="true"  ng-selected="true" value="">Select a state</option>
  </select>


    $scope.update=function(index){
      for(var i=0;i<countryOptions.length;i++){
        if(countryOptions.name == index){
          $scope.index= index;
        }
      }
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多