Angular select with ng-option sends index instead of value when posting form Angular select with ng-option sends index instead of value when posting form angularjs angularjs

Angular select with ng-option sends index instead of value when posting form


You shouldn't be serializing your form with jquery since you have all of the pertinent data in the $scope and that's what you'd want to serialize (some subset or some object that you'd be posting with $http or $resource). If you are dead set with your approach, create a hidden input with the real name of the data element: http://jsfiddle.net/2SuZG/1/

<select name="resourceTemp" ng-options="r for r in ['a', 'b']"        ng-model="selectedResource"></select><input type="hidden" name="resource" value="{{selectedResource}}" >

I wouldn't recommend this but it is what you'll want to do.


ng-option works on array. if you want to use ng-option make array like this

r = [{ "value": 1, "text": "a" }, { "value": 2, "text": "v" }];  <select ng-option="obj.value as obj.text for obj in r">

Or you can use ng-repeat in your case

<form method="post">        <select name="resourceTemp" ng-model="selectedResource">            <option ng-repeat="r in ['a','b']" value="{{r}}">{{r}}</option>        </select>        <input type="hidden" name="resource" value="{{selectedResource}}" >        <button type="submit">Save</button></form>

http://jsfiddle.net/nHyET/


The simple answer is to not post forms. If you want to POST data to an endpoint, use $http or $resource.

Check out Mark Rajcok's comment on the docs: http://docs.angularjs.org/api/ng.directive:select