ng-options重复错误?

时间:2015-08-10 14:04:20

标签: javascript jquery angularjs

我附上了我需要显示的代码

$scope.states="India";    
$scope.cities="Madhya Pradesh"; 
$scope.city="Ajmer"; 

这些数据在级联下拉列表中,当我提出错误时,我已将jsfiddle括起来。

期望:  已经入选印度,Madhya Pradesh,Ajmer。我想在级联下拉列表中显示这些数据

代码:

angular.module('test', [])
  .controller('TestController', ['$scope',
      function($scope) {
	  $scope.states='India';
	  $scope.cities='Maharashtra';
	  $scope.city='Mumbai';
	  
	  $scope.countries = {
          'India': {
            'Maharashtra': ['Pune', 'Mumbai', 'Nagpur', 'Akola'],
            'Madhya Pradesh': ['Indore', 'Bhopal', 'Jabalpur'],
            'Rajasthan': ['Jaipur', 'Ajmer', 'Jodhpur']
          },
          'USA': {
            'Alabama': ['Montgomery', 'Birmingham'],
            'California': ['Sacramento', 'Fremont'],
            'Illinois': ['Springfield', 'Chicago']
          },
          'Australia': {
            'New South Wales': ['Sydney'],
            'Victoria': ['Melbourne']
          }
        };
	  
	  
        

        $scope.getCountry = function(val) {
          for (var key in $scope.countries) {
            if ($scope.countries.hasOwnProperty(key)) {
              if ($scope.countries[key] === val) {
                alert('You selected: ' + key);
              }
            }
          }
        };
        
        $scope.getCity = function(city, state) {
          for (var key in state) {
            if (state.hasOwnProperty(key)) {
              if (state[key] === city) {
                alert('You selected: ' + key);
              }
            }
          }
        };

        $scope.alertCity = function(city) {
          alert('You selected ' + city);
        };
  }]);
<div ng-app="test">
  <div ng-controller="TestController">
    <div>
      Country:
      <select id="country" ng-model="states" ng-options="country for (country, states) in countries track by $index" ng-change="getCountry(states)">
        <option value=''>Select</option>
      </select>
    </div>
    <div>
      States:
      <select id="state" ng-disabled="!states" ng-model="cities" ng-options="state for (state,city) in states" ng-change="getCity(cities, states)">
        <option value=''>Select</option>
      </select>
    </div>
    <div>
      City:
      <select id="city" ng-disabled="!cities || !states" ng-model="city" ng-change="alertCity(city)">
        <option value=''>Select</option>
        <option ng-repeat="city in cities" value="{{city}}">{{city}}</option>
      </select>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

我会按以下方式进行:

JS:
1.重组您的JSON数据 2.重命名您的方法来描述他们的行为。

angular.module('test', [])
  .controller('TestController', ['$scope','$filter',
      function($scope, $filter) {
      $scope.country="India";
      $scope.state="Rajasthan";
      $scope.city="Ajmer";

        $scope.countries = [
    {
        "name": "India",
        "states": [
            {
                "name": "Maharashtra",
                "cities": [
                    {"name":"Pune"},
                    {"name":"Mumbai"},
                    {"name":"Nagpur"},
                    {"name":"Akola"}
                ]
            },
            {
                "name": "Madhya Pradesh",
                "cities": [
                    {"name":"Indore"},
                     {"name":"Bhopal"},
                    {"name":"Jabalpur"}
                ]
            },
            {
                "name": "Rajasthan",
                "cities": [
                    {"name":"Jaipur"},
                    {"name":"Ajmer"},
                    {"name":"Jodhpur"}
                ]
            }
        ]
    },
    {
        "name": "USA",
        "states": [
            {
                "name": "Alabama",
                "cities": [
                    {"name":"Montgomery"},
                    {"name":"Birmingham"}
                ]
            },
            {
                "name": "California",
                "cities": [
                    {"name":"Sacramento"},
                     {"name":"Fremont"}
                ]
            },
            {
                "name": "Illinois",
                "cities": [
                    {"name":"Springfield"},
                    {"name":"Chicago"}
                ]
            }
        ]
    },
    {
        "name": "Australia",
        "states": [
            {
                "name": "NewSouthWales",
                "cities": [
                    {"name":"Sydney"}
                ]
            },
            {
                "name": "Victoria",
                "cities": [
                    {"name":"Melbourne"}
                ]
            }
        ]
    }
];

        $scope.getStates = function() {
            $scope.states = null;
            $scope.cities = null;
            console.log($scope.country);
            if($scope.country != null){
                var filteredCountry = $filter("filter")($scope.countries, $scope.country);
                if(filteredCountry != null && filteredCountry.length == 1){
                    $scope.states = filteredCountry[0].states;
                }             
            }
        };

        $scope.getCity = function() {
            $scope.cities = null;
          if($scope.state != null){
              var filteredState = $filter("filter")($scope.states, $scope.state);
                if(filteredState != null && filteredState.length == 1){
                    $scope.cities = filteredState[0].cities;
                }               
            }
        };

        if($scope.country != null){
            var filteredCountry = $filter("filter")($scope.countries, $scope.country);
            if(filteredCountry != null && filteredCountry.length == 1){
                 $scope.states = filteredCountry[0].states;

                var filteredState = $filter("filter")($scope.states, $scope.state);
                if(filteredState != null && filteredState.length == 1){
                    $scope.cities = filteredState[0].cities;
                } 
            }
        }

        $scope.alertCity = function() {
          alert('You selected ' + $scope.city);
        };
          //console.log($scope.countries);
  }]);

HTML:

    <div ng-app="test">
  <div ng-controller="TestController">
    <div>
      Country:
      <select ng-model="country" ng-options="country.name as country.name for country in countries" ng-change="getStates()">
        <option value=''>Select</option>
      </select>
    </div>
    <div>
      States:
      <select id="state" ng-disabled="!country" ng-model="state" ng-options="state.name as state.name for state in states" ng-change="getCity()">
        <option value=''>Select</option>
      </select>
    </div>
    <div>
      City:
      <select id="city" ng-disabled="!country || !state" ng-model="city" ng-options="city.name as city.name for city in cities" ng-change="alertCity()">
        <option value=''>Select</option>
      </select>
    </div>
  </div>
</div>

希望这有帮助