繁体   English   中英

如何使用另一个ng-option选择值动态更新ng-option值?

[英]How to update ng-option value dynamically with another ng-option selected value?

我的应用程序中有2个选择选项框。 第一个选择选项框包含国家/地区的名称。 我想根据在第一个选择框中选择的值更新第二个选择选项框值。

例如,如果我选择印度,则第二个选择框值应包含印度的所有州。 其他国家也是如此,但我无法这样做。

码:

<select ng-model="selectedCountry" ng-options="item as item for item in country">
    <option ng-if="!selectedCountry" value="">Select Country</option>
</select>
<pre>selectedItem: {{selectedItem | json}}</pre>

<select ng-model="selectedState" ng-options="{{selectedState}}">
    <option ng-if="!selectedState" value="">Select state</option>
</select>
<pre>selectedItem: {{selectedState | json}}</pre>

JS:

$scope.country = [
         "India",
         "America",
        "Nepal",
         "China"
    ];
  $scope.India = ["Bihar"];
    $scope.America = ["Arizona"];
    $scope.China = ["Beging"];
    $scope.Nepal = ["Dhankuta"];

你可以让你的国家,如数组的键countries.india等在第二个选择,你可以设置NG-选项从重复countries是选择相匹配的选项键selectedCountry在第一个选择框。 就像印度被选中一样,将重复通过关键的'印度' countries阵列

演示

 angular.module('test', []) .controller('testController', function($scope) { $scope.country = [ "India", "America", "Nepal", "China" ]; $scope.countries = []; $scope.countries.India = ["Bihar", "mumbai"]; $scope.countries.America = ["Arizona"]; $scope.countries.China = ["Beging"]; $scope.countries.Nepal = ["Dhankuta"]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="testController"> <select ng-model="selectedCountry" ng-options="item as item for item in country"> <option ng-if="!selectedCountry" value="">Select Country</option> </select> <pre>selectedItem: {{selectedCountry}}</pre> <select ng-model="selectedState" ng-options="items for items in countries[selectedCountry]"> <option ng-if="!selectedCountry" value="">Select Country</option> </select> <pre>selectedItem: {{selectedState }}</pre> </div> 

你应该使用select的ng-change属性。

<select ng-change="changeStates()">...</select>

$scope.changeStates = function(){
    $scope['currentStates'] = $scope[$scope.selectedCountry];
};

<select ng-options="state as state for state in currentStates">...</select>

但在我看来,你应该更好地使用id值

编辑:你不应该直接将国家注入范围。 scope.States.America会更好

我使用$ watch函数看看演示点击这里

 <body ng-app ng-controller="AppCtrl"> <select ng-model="selectedCountry" ng-options="item as item for item in country">
        <option value="">Select Country</option> </select> <pre>selectedItem: {{selectedCountry | json}}</pre>

    <select ng-model="selectedState" ng-options="item as item for item in state">
        <option ng-if="!selectedState" value="">Select state</option> </select> <pre>selectedItem: {{selectedState | json}}</pre> </body>

JavaScript的:

function AppCtrl($scope) {

$scope.country = ["India", "America", "Nepal","China" ];
var state = {India :["Bihar"],America :["Arizona"],China:["Beging"],Nepal:["Dhankuta"]};
    $scope.$watch('selectedCountry', function(newValue, oldValue) {
      if ( newValue) {
      $scope.state = state[$scope.selectedCountry];
    }
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM