[英]Default option for select not working with boolean value
有人可以幫忙解釋一下為什么第一個和最后一個選擇不顯示默認值嗎?
angular.module('myApp', []); angular.bootstrap(document.documentElement, ['myApp']);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> <div ng-init="options=[{id: false, label: 'False'}, {id: true, label: 'True'}]"></div> <select ng-init="myVar=false" ng-model="myVar" > <option ng-value="false">False</option> <option ng-value="true">True</option> </select> <select ng-init="myVar2=false" ng-model="myVar2" ng-options="option.id as option.label for option in options"> </select> <select ng-init="myVar3=false" ng-model="myVar3" ng-options="option.id as option.label for option in options track by option.id"> </select>
要從ngOptions
文檔頁面解釋:
在兩個示例中,
track by
表達式track by
成功地應用於了options
數組中的每個option
。 由於已在控制器中以編程方式設置了所選選項,因此track by
表達式track by
也將應用於ngModel
值。 當ngModel
值是options[0]
且track by
表達式track by
為options[0].id
,沒有問題。 當ngModel
值為options[0].label
ngModel
且track by
表達式track by
為options[0].label.id
(未定義)。 結果,模型值與任何<option>
不匹配,並且<select>
似乎沒有選定值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.