javascript - Selected value for angular select not be set on page refresh -
the selected value of angularjs select not being set on page refresh.
- it is set if navigate different view , navigate again.
- the model select saved local storage , initialized scope onload.
- i have checked data in scope same in 2 situations of navigating away , again, , refreshing page. data same.
after lot of investigation, see when select pristine, or when page refreshed, select list have blank option, first 1 below value ?
:
<select ng-model="question.answer" class="form-control" ng-options="opt opt.value opt in question.options" > <option value="?" selected="selected"></option> <option value="0">1</option> <option value="1">2</option> </select>
note above html output. angular view has:
<select ng-model="question.answer" class="form-control" ng-options="opt opt.value opt in question.options" />
i'm not sure why preventing correct option being selected correct object still present @ question.answer. see simplified version of model below:
var question = { options: [ { id: 1, value: "1" }, { id: 2, value: "2" } ], answer: { id: 2, value: "2" } };
my understanding in ng-options
, first opt
specifies value should assigned variable specified in ng-model
. in other words, assigning object in options array question.answer
, rather value. can see looking @ model case.
but i'm not sure how angular use value in question.model
determine option set selected.
i wonder if problem fact values of options set indices of question.options , can't work out question.answer
index answer is.
so, guess questions are: - why correct option not being set when <option value="?" selected="selected"></option>
present? - how angular determine how use model set selected option (aka - funny indices in option values?)
sorry post long. thank reading.
update: using chrome angularjs batarang extension, value being set question.answer
picklist e.g.:
{ $ref: $["options"][1] }
which means value of picklist "2".
so how using indices of options being used, how have set model? i.e. using $ref
etc?
i edited answer after looked @ doing.
the ng-options attribute has track expression you're talking about:
ng-options="opt opt.value opt in question.options track opt.id"
see updated fiddle: http://jsfiddle.net/ckleu/6/
Comments
Post a Comment