Angularjs触发国家依赖

Scr*_*ion 6 javascript watch angularjs

有人可以帮助我做一个国家/国家下拉依赖工作的例子吗?

我故意以这种方式创建JSON,因为我希望依赖项是通用的,所以我可以在任何下拉列表中应用它,同时只使用Meta Data而不是HTML.

这是一个链接, 可以看到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>
Run Code Online (Sandbox Code Playgroud)

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];
});
Run Code Online (Sandbox Code Playgroud)

}

Dot*_*Dot 7

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

          {"items": [{  "id": "201",
                    "name": "Alberta"
                   }, .....
Run Code Online (Sandbox Code Playgroud)

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

 <select data-ng-model="selectedCountry">
            <option ng-repeat='country in Countries.items' value='{{country.StateGroupID}}'>{{country.name}}</option>          
        </select>
Run Code Online (Sandbox Code Playgroud)

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

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

然后您可以使用此功能使用ng-repeat显示它们

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

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


mad*_*ead 7

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

$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
}];
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

$scope.updateCountry = function(){
  $scope.availableStates = [];

  angular.forEach($scope.states, function(value){
    if(value.countryId == $scope.country.id){
      $scope.availableStates.push(value);
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

就这样.这是一个适合你的工作插头.

  • 如果将countryCode添加到状态,则使用过滤器要简单得多:ng-options ="state.name for state in states | filter:{country:countryCode}" (2认同)

zs2*_*020 6

最简单的修复方法是参考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">
Run Code Online (Sandbox Code Playgroud)

Demo