【问题标题】:Angularjs trigger country state dependencyAngularjs触发国家状态依赖
【发布时间】:2013-08-12 23:32:45
【问题描述】:

有人可以帮我制作我的国家/州下拉依赖项示例吗?

我有意以这种方式创建 JSON,因为我希望依赖项是通用的,因此我可以在仅使用元数据而不是 HTML 的情况下将其应用于任何下拉列表。

这里a link查看JSFidlle中的代码示例

HTML

Country:<select data-ng-model="Countries" data-ng-options="country.id as country.name for country in Countries.items">
            <option value="">Please select a country</option>
        </select>

State: <select data-ng-model="currentItem" data-ng-options="item.id as item.name for item in currentStates.items">
            <option value="">Please select a state</option>
        </select>

JavaScript 代码:

function Controller($scope) {

var Countries = {
    "id": "field10",
    "items": [{
                "id": "10",
                "StateGroupID":"0",
                "name": "United State"
              }, 
              {
                 "id": "2",
                 "StateGroupID":"1",
                 "name": "Canada"
              }]
};

var States =
    {   "id": "field20",
        "StateGroups": [{
            "items": [{  "id": "1",
                       "name": "Alabama"
                      }, 
                      {
                          "id": "2",
                          "name": "Alaska"
                      }, 
                      {  "id": "3",
                       "name": "Arizona"
                      }, 
                      {  "id": "4",
                       "name": "California"
                      }]},

                 [{  "id": "201",
                    "name": "Alberta"
                   }, 
                  {
                      "id": "202",
                      "name": "British Columbia"
                  }, 
                  {
                      "id": "303",
                      "name": "Manitoba"
                  }, 
                  {
                      "id": "304",
                      "name": "Ontario"
                  }]]
};

$scope.Countries = Countries;
$scope.currentStates = States.StateGroups[0];
$scope.$watch('currentStates', function(value, oldValue){
    //alert(value);
    //alert(JSON.stringify(value));
    //$scope.currentStates = (value == "10") ?  States.StateGroups[0] : States.StateGroups[1];
});

}

【问题讨论】:

    标签: javascript angularjs watch


    【解决方案1】:

    角度国家选择器 bower 安装 angular-country-picker(或) npm install angular-country-picker

    <script src="bower_components/angular-country-picker/country-picker.js"></script>
    <script src="node_modules/angular-country-picker/country-picker.js"></script>
    
           angular.module('webApp', ['puigcerber.countryPicker']);
    
           <select ng-model="selectedCountry" pvp-country-picker="name"></select>
    
           angular.module('myApp', ['puigcerber.countryPicker'])
           .config(function(pvpCountriesProvider) {
            pvpCountriesProvider.setCountries([
            { name: 'Abkhazia', alpha2: 'AB'},
            { name: 'Kosovo', alpha2: 'XK'},
            { name: 'Nagorno-Karabakh', alpha2: 'NK'},
            { name: 'Northern Cyprus', alpha2: 'KK'},
            { name: 'Somaliland', alpha2: 'JS'},
            { name: 'South Ossetia', alpha2: 'XI'},
            { name: 'Transnistria', alpha2: 'PF'}
          ]);
        });
    

    【讨论】:

      【解决方案2】:

      最简单的修复方法是在第二次选择中引用currentCountry,无需使用$watch 即可满足您的要求。

      <select data-ng-model="currentCountry" data-ng-options="country.name for country in Countries.items">
      
      <select data-ng-model="currentItem" data-ng-options="item.id as item.name for item in States.StateGroups[currentCountry.StateGroupID].items">
      

      Demo

      【讨论】:

      • 您的回答很好,但是您更改了 HTML,而我特别提到我想保持当前 HTML 不变并使用 JavaScript。
      • @scription 我在你的问题中看不到这一点。你提到了一些关于数据结构的东西,但这与 HTML 完全不同。
      【解决方案3】:

      我建议你对你的数据模型进行一些重构——它看起来很纠结。让我们将县和州存储在两个数组中:

      $scope.countries = [{
          "name": "USA",
          "id": 1
        },{
          "name": "Canada",
          "id": 2
      }];
      $scope.states = [{
          "name": "Alabama",
          "id": 1,
          "countryId": 1
        }, {
          "name": "Alaska",
          "id": 2,
          "countryId": 1
        }, {
          "name": "Arizona",
          "id": 3,
          "countryId": 1
        }, {
          "name": "Alberta",
          "id": 4,
          "countryId": 2
        }, {
          "name": "British columbia",
          "id": 5,
          "countryId": 2
      }];
      

      有了这个,我们可以为数据写selects:

      <select data-ng-model="country" data-ng-options="country.name for country in countries" data-ng-change="updateCountry()">
        <option value="">Select country</option>
      </select>
      <select data-ng-model="state" data-ng-options="state.name for state in availableStates">
        <option value="">Select state</option>
      </select>
      

      很遗憾我们不能在选择器中使用if 表达式——如果可以的话,我们不需要一行JS!但我们需要:

      $scope.updateCountry = function(){
        $scope.availableStates = [];
      
        angular.forEach($scope.states, function(value){
          if(value.countryId == $scope.country.id){
            $scope.availableStates.push(value);
          }
        });
      }
      

      仅此而已。 Here is a working plunk for you.

      【讨论】:

      • 如果国家代码被添加到州,使用过滤器要简单得多:ng-options="state.name for state in states | filter:{country:countryCode}"
      • 可能应该使用 value.countryId === $scope.country.id 作为其严格相等性。
      【解决方案4】:

      首先,我认为你的 JSON 有一个小错误,你应该在加拿大各州之前有一个“项目”

                {"items": [{  "id": "201",
                          "name": "Alberta"
                         }, .....
      

      完成此操作后,我将修改您的 HTML 以获得 2 个不同的模型名称(按照您的方式,在第一次单击时覆盖国家/地区列表)。然后我将为 ng-repeat 使用不同的语法,以便将值强制为 StateGroupId

       <select data-ng-model="selectedCountry">
                  <option ng-repeat='country in Countries.items' value='{{country.StateGroupID}}'>{{country.name}}</option>          
              </select>
      

      这样做可以让您创建一个函数来获取所选组 ID 的状态:

       $scope.getStates=function(){
          console.log($scope.selectedCountry)
           return $scope.backupStates.StateGroups[$scope.selectedCountry].items;
      }
      

      然后你可以使用这个函数通过 ng-repeat 来显示它们

                  <select data-ng-model="selectedState" >
                    <option value="">Please select a state</option>
                    <option ng-repeat='state in getStates()'>{{state.name}}</option>
                  </select>
      

      我在这里修改了你的小提琴:http://jsfiddle.net/DotDotDot/TsxTU/14/,我希望这是你想要的那种行为:)

      【讨论】:

      • 您的回答对我很有用。您已将我引导至 JSON 结构中的错误以及帮助我​​更好地理解我的错误以及如何修复它们并最终使我的示例正常工作的 HTML。你的例子比我的好,但是你为什么使用 [ng-repeat] 而不是 [ng-options]
      • 很高兴它对您有所帮助。我使用的第一个 ng-repeat 主要是强制每个项目的 value 属性,我发现这在测试时更容易修改,但您可以使用 ng-options 使其工作。第二个,我很懒,我复制/粘贴了上面 ng-repeat 3 行的很大一部分;)。所以,简而言之,如果你愿意,你可以使用 ng-options,它也可以工作:)
      猜你喜欢
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多