At first I tried <select name='size' ng-model='sid' ng-options='y.id as y.title for y in arr'>
. But when the form was submitted, the posted variable size
is not the id 472 473 474
but the index 0 1 2
.
Then I tried ng-options='y.title for y in arr track by y.id'
. But the selected model sid
is not 473
but {"id":473,"title":"bb"}
so I can't make that 473 item as default by ng-init='sid = 473'
.
At last, I have to use option ng-repeat='y in arr'
as below. But it seems that ng-init is useless, because it will initialize before the options repeat. How can I make it work?
Thanks.
<div ng-controller="Ctrl">
<select ng-model='sid' ng-init='sid = 473'>
<option ng-repeat='y in arr' value='{{ y.id }}'>
{{ y.id+' - '+y.title }}
</option>
</select>
{{ sid }}
<button ng-click='sid=474'>change to 474</button>
</div>
<script>
var app =angular.module('app', []);
function Ctrl($scope){
$scope.arr = [{"id":472,"title":"aa"},{"id":473,"title":"bb"},{"id":474,"title":"cc"}];
}
</script>