级联下拉angularjs ng-option对象错误 [英] cascading dropdown angularjs ng-option object error
问题描述
我用过这个例子 http://devzone.co.in/simple-example-of-dependable-dropdowns-cascading-dropdowns-using-angularjs/ 这里工作正常,但我用索引号获取对象而不是获取值
我的控制器'使用严格';
/*** @ngdoc 对象* @name test.Controllers.TestController* @description 测试控制器* @requires ng.$scope*/有角的.module('测试').controller('测试控制器', ['$范围',功能($范围){$scope.countries = {'印度': {马哈拉施特拉邦":[浦那"、孟买"、那格浦尔"、阿科拉"]、'中央邦':['印多尔'、'博帕尔'、'贾巴尔普尔'],'拉贾斯坦邦':['斋浦尔'、'阿杰梅尔'、'焦特布尔']},'美国': {'阿拉巴马':['蒙哥马利','伯明翰'],'加利福尼亚':['萨克拉门托','弗里蒙特'],'伊利诺伊州':['斯普林菲尔德'、'芝加哥']},'澳大利亚': {'新南威尔士':['悉尼'],'维多利亚':['墨尔本']}};$scope.GetSelectedCountry = 函数 () {$scope.strCountry = document.getElementById("country").value;var datas =$scope.strCountry;console.log(JSON.stringify(datas));};$scope.GetSelectedState = 函数 () {$scope.strState = document.getElementById("state").value;};}]);
我的查看页面
国家:选择 状态:选择 城市:选择{{城市}} 所选国家:{{strCountry}} 选定状态:{{strState}} 选择的城市:{{城市}}如果只想获取选中的国家和州,可以遍历对象,检查哪个键与 ng-model 的值匹配.要获得城市,您只需返回 ng-model 的值.
angular.module('test', []).controller('TestController', ['$scope',功能($范围){$scope.countries = {'印度': {马哈拉施特拉邦":[浦那"、孟买"、那格浦尔"、阿科拉"]、'中央邦':['印多尔'、'博帕尔'、'贾巴尔普尔'],'拉贾斯坦邦':['斋浦尔'、'阿杰梅尔'、'焦特布尔']},'美国': {'阿拉巴马':['蒙哥马利','伯明翰'],'加利福尼亚':['萨克拉门托','弗里蒙特'],'伊利诺伊州':['斯普林菲尔德'、'芝加哥']},'澳大利亚': {'新南威尔士':['悉尼'],'维多利亚':['墨尔本']}};$scope.getCountry = function(val) {for (var key in $scope.countries) {如果 ($scope.countries.hasOwnProperty(key)) {if ($scope.countries[key] === val) {alert('您选择了:' + key);}}}};$scope.getCity = 函数(城市,州){for (var key in state) {如果 (state.hasOwnProperty(key)) {如果(状态[键] === 城市){alert('您选择了:' + key);}}}};$scope.alertCity = 函数(城市){alert('你选择了'+城市);};}]);
<script src="https://code.angularjs.org/1.3.15/angular.min.js"><<;/脚本><div ng-app="测试"><div ng-controller="TestController"><div>国家:<select id="country" ng-model="states" ng-options="country for (country, states) in countries" ng-change="getCountry(states)"><option value=''>选择</option></选择>
<div>状态:<select id="state" ng-disabled="!states" ng-model="cities" ng-options="state for (state,city) in states" ng-change="getCity(cities, states)"><option value=''>选择</option></选择>
<div>城市:<select id="city" ng-disabled="!cities || !states" ng-model="city" ng-change="alertCity(city)"><option value=''>选择</option><option ng-repeat="城市中的城市" value="{{city}}">{{city}}</option></选择>