It seems that it is virtually impossible to use the βvalueβ of the selection in any meaningful way as a regular HTML form element, and also connect it to Angular in an approved way using ng-options. As a compromise, I had to put a hidden input next to my choice, and it keeps track of the same model as my choice, like this (all this is very simplified from real production code for short):
HTML:
<select ng-model="profile" ng-options="o.id as o.name for o in profiles" name="something_i_dont_care_about"> </select> <input name="profile_id" type="text" style="margin-left:-10000px;" ng-model="profile"/>
JavaScript:
App.controller('ConnectCtrl',function ConnectCtrl($scope) { $scope.profiles = [{id:'xyz', name:'a profile'},{id:'abc', name:'another profile'}]; $scope.profile = -1; }
Then in my server code, I just looked for params[:profile_id] (it was a Rails application, but the same principle applies anywhere). Since the hidden input tracks the same model as the selection, they remain synchronized automatically (no additional javascript is required for this). This is the coolest part of Angular. This almost compensates for what it does with the value attribute as a side effect.
Interestingly, I found that this method only worked with input tags that were not hidden (so I had to use margin-left: -10000px; a trick to move input from the page). These two options did not help:
<input name="profile_id" type="text" style="display:none;" ng-model="profile"/>
and
<input name="profile_id" type="hidden" ng-model="profile"/>
I feel this should mean that I'm missing something. It seems too strange that this is a problem with Angular.
Tim Cull Aug 29 '13 at 5:02 2013-08-29 05:02
source share