誰能告訴我,爲什麼在第一個例子模型選項被選中,而第二個不爲普通數組:AngularJS 1.5 ngOptions的比較顯示普通數組
// Plain old array
vm.owner = ['example', 'example2', 'example3', ...];
在機型vm.model.address.owner = 2;
// EXAMPLE 1 - Works
// Force index to be a number: using id*1 instead of it being a string:
// and the option ('example3') is selected based on the model value of 2
// indicating the index
<select id="owner"
name="owner"
placeholder="Residential Status"
ng-model="vm.model.address.owner"
ng-required="true"
ng-options="id*1 as owner for (id, owner) in vm.owner">
<option value="">Select Value</option>
</select>
嘗試不使用hack並使用替代索引2的軌道即使該值仍在模型中設置,但未被選中。
// EXAMPLE 2 - Doesn't Work
// Markup doesn't show value to be a string: using track by, but the
// option set in the model doesn't cause the option to be selected it
// remains as the default with a value of ''
<select id="owner"
name="owner"
placeholder="Residential Status"
ng-model="vm.model.address.owner"
ng-required="true"
ng-options="owner for (id, owner) in vm.owner track by id">
<option value="">Select Value</option>
</select>
我發現ngOptions是超級混亂如此例如2任何解釋或解決方案,因爲它是清潔,不是黑客攻擊將是巨大的。