Situation:
I have the following choice:
<select ng-model="model" ng-options="o as o.name for o in options track by o.code">
<option value="">- Default -</option>
</select>
My tags optionsare as follows:
$scope.options = [{id: 1, code: 'foo', name: 'Foo!'}, {id: 2, code: 'bar', name: 'Bar!'}];
What I want to do:
I want to have my choice with a pre-selected value. My limitation is that I only know the attribute of codemy object. With the sign, track byI can do it just like this:
$scope.model = {code: 'bar'};
And it works, the selected value of select is "Bar!"
Problem:
When I send this data to my server, I need to send an attribute of idmy object. Transmitted data {code: 'bar'}, but not {id: 2, code: 'bar', name: 'Bar!'}as I want.
This is normal behavior for me ... because I saved in my model {code: 'bar'}and I did not change the selected value.
Conclusion:
AngularJS options model, model ( track by)?
: , - $scope.model = $scope.options[2] ( ...), - ... ...: D