【发布时间】:2015-05-05 07:59:09
【问题描述】:
我应该有以下代码:
.JS
angularcomponents.controller('CountryCntrl', ['$scope', function($scope) {
$scope.countries = {
'India': {
'Maharashtra': ['Pune', 'Mumbai', 'Nagpur', 'Akola'],
'Madhya Pradesh': ['Indore', 'Bhopal', 'Jabalpur'],
'Rajasthan': ['Jaipur', 'Ajmer', 'Jodhpur']
},
'USA': {
'Alabama': ['Montgomery', 'Birmingham'],
'California': ['Sacramento', 'Fremont'],
'Illinois': ['Springfield', 'Chicago']
},
'Australia': {
'New South Wales': ['Sydney'],
'Victoria': ['Melbourne']
}
};
}]);
和html:
<div ng-controller="CountryCntrl">
<div class="col-sm-4">
<select id="country" ng-model="states" ng-options="country for (country, states) in countries">
<option value=''>
Select
</option>
</select>
<br>
<small><em>Country</em></small>
</div>
<div class="col-sm-4">
<select id="state" ng-disabled="!states" ng-model="cities" ng-options="state for (state,city) in states">
<option value=''>
Select
</option>
</select>
<br>
<small><em>States</em></small>
</div>
<div class="col-sm-4">
<select id="city" ng-disabled="!cities || !states" ng-model="city">
<option value=''>
Select
</option>
<option ng-repeat="city in cities" value='[[city]]'>
[[city]]
</option>
</select>
<br>
<small><em>City</em></small>
</div>
</div>
我不明白它如何为城市、国家和州绑定正确的价值观。在我的 $scope 中没有任何标签可以识别它。
是否取决于数组的格式?
【问题讨论】:
-
你能给我一个提琴手演示吗?
-
也许问题有点不清楚,你是在问为什么选择框绑定到正确的城市、州和国家?您的对象是使用关系创建的这一事实意味着它们是?
标签: angularjs angularjs-directive angular-ngmodel