我正在研究一對內容相互關聯的組合。如何擺脫依賴選擇的默認值?
HTML
<span>{{lblRegion}}</span>
<select ng-model="currentRegion"
ng-options="region.name for region in regions"
ng-change="checkAll()">
</select>
<span>{{lblCountry}}</span>
<select ng-model="currentCountry"
ng-options="country.name for country in currentRegion.countries"
ng-disabled="currentRegion.id===0">
</select>
JS
$scope.regions = regions;
$scope.currentRegion = regions[0];
$scope.currentCountry = $scope.currentRegion.countries[0];
$scope.checkAll = function() {
if (currentRegion.id !== 0) {
$scope.test = true;
} else {
$scope.test = false;
}
}
var regions = [{
'id': 0,
'name': 'All',
'countries': [{
'id': '0',
'name': 'All'
}]
},
...
行爲必須滿足這樣的:
- 兩個功放音箱的 '全部' 選項。
- 「區域」組合的內容設置「國家」組合的可能值。
- 如果區域組合設置爲「全部」,則國家/地區組合會被禁用,其內容爲「全部」選項。
我想擺脫總是出現在國家組合上的空白默認選項。我讀過ng-init
可以完成這項工作,但ng-init docs指出不應該以這種方式管理這個問題。
我怎樣才能刪除空的選項?
我喜歡你的答案......但我想知道是否有機會逃避使用$ watch ... – JPCF
@JPCF你總是可以使用'ng-change =「currentCountry = currentRegion.countries [0]「,儘管它會隱藏模板中的一些邏輯。 –
是的......這是另一種選擇......我希望它完全是聲明性的......但是,'根據我們與@Maxim Shoustin的討論,如果它尋找老鼠'貓的顏色並不重要' – JPCF