层叠表格要求验证不起作用?

时间:2015-08-21 08:07:08

标签: javascript jquery angularjs

这里我正在使用国家,州,城市的级联下拉。如果用户选择国家将根据州将来,并根据州选择城市将来。我想验证国家,州,城市。我已经把正常的要求,为了显示错误信息,我使用了ng-message,但它没有工作,有人帮助我



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);
  }]);

<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>
&#13;
&#13;
&#13;

0 个答案:

没有答案