我想使我的selectbox可靠,因此,如果我选择区域“ America”,则第二个selectBox应该仅显示纽约,但我不知道如何实现,我尝试在选项中使用ng-click ,但无效。而且我无法更改数据库类型,需要使用它,因此任何人都知道如何在不更改任何数据库结构的情况下进行创建?



var app = angular.module('app', []);

app.controller('ctrl', function($scope) {
$scope.countryList=[];
	$scope.data =[
	{id:1, country_code:"KR", country_name:"Korea", city_name:"Busan", city_code:"PUS"},
	{id:1, country_code:"KR", country_name:"Korea", city_name:"Seoul", city_code:"SEL"},
	{id:1, country_code:"KR", country_name:"Korea", city_name:"Ulsan", city_code:"USN"},
	{id:1, country_code:"KR", country_name:"Korea", city_name:"GwangJu", city_code:"KWJ"},
	{id:1, country_code:"KR", country_name:"Korea", city_name:"Gunsan", city_code:"KUV"},
	{id:1, country_code:"USA", country_name:"America", city_name:"New York", city_code:"NY"}
	];


 			for (var i = 0; i < $scope.data.length; i++) {
 				var isExist = false;
 				for (var j = 0; j < $scope.countryList.length; j++) {
 					if (JSON.stringify($scope.data[i].country_code) == JSON.stringify($scope.countryList[j].country_code)) {
 						isExist = true;
 						break;
 					}
 				}
 				if (isExist == false) {
 					$scope.countryList.push($scope.data[i]);
 				}
 			}
});
      

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div class="port_small_select_div">
            <select class="small_select">
              <option disabled selected hidden>Region</option>
              <option ng-repeat="country in countryList" value="{{ country.country_code }}">{{ country.country_name}}</option>
            </select>
          </div>

          <div class="port_select_div">
            <select class="big_select">
              <option disabled selected hidden>Detailed information</option>
              <option ng-repeat="city in data" value="{{ city.city_code }}">{{ city.city_name}}</option>
            </select>
          </div>
          </div>

最佳答案

您可以根据第一个下拉菜单的选择country_code在第二个下拉菜单上应用过滤器。我还替换了下拉列表以使用ng-options。另外,您应该考虑在每个选择框中添加ng-model

<select class="small_select" ng-model="selectedRegion"
  ng-options="country.country_name as country_code for in countryList">
  <option disabled selected hidden>Region</option>
</select>

<select class="big_select" ng-model="selectedCity"
 ng-options="city.city_code as city.city_name for city in data | filter: {country_code: selectedRegion }">
  <option disabled selected hidden>Detailed information</option>
</select>

关于javascript - angularjs ng-repeat可靠的selectBox,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44429249/

10-09 23:03