cascading dropdown angularjs ng-option object error

后端 未结 1 427
余生分开走
余生分开走 2020-12-22 11:25

i have used this example http://devzone.co.in/simple-example-of-dependable-dropdowns-cascading-dropdowns-using-angularjs/ here its working fine but my getting object with in

相关标签:
1条回答
  • 2020-12-22 12:03

    If you just want to get the selected country and state, can iterate over the object and check which key matches the value of ng-model. To get the city, you only have to return the value of ng-model.

    angular.module('test', [])
      .controller('TestController', ['$scope',
          function($scope) {
            $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);
            };
      }]);
    <script src="https://code.angularjs.org/1.3.15/angular.min.js"></script>
    
    <div ng-app="test">
      <div ng-controller="TestController">
        <div>
          Country:
          <select id="country" ng-model="states" ng-options="country for (country, states) in countries" 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>

    0 讨论(0)
提交回复
热议问题