<select ng-options> binding to array: how to set option values?

8,725 views
Skip to first unread message

ehahn

unread,
Nov 12, 2012, 12:51:25 PM11/12/12
to ang...@googlegroups.com
I'm embarrassed to say I'm having trouble getting my <options value=xxx> to be set correctly when binding to an array, e.g (trivial, contrived example) to have the value match the label:

<select ng-model="result" ng-options="value as value for value in [123, 456]"></select>

I'd like to see:

<option value="123">123</option>

but I always see:

<option value="-index-">123</option>


BTW, works fine for object binding, just not array binding.  Thanks.

avvensis

unread,
Nov 12, 2012, 1:08:35 PM11/12/12
to ang...@googlegroups.com
"value for value in values"
http://jsfiddle.net/avvensis/fq3PW/

More info about ngOptions you can find in documentation http://docs.angularjs.org/api/ng.directive:select at section Parameters.

понедельник, 12 ноября 2012 г., 23:51:25 UTC+6 пользователь ehahn написал:

Eric Hahn

unread,
Nov 13, 2012, 5:47:38 PM11/13/12
to ang...@googlegroups.com
duh!  I (thought I) had tried that and upon inspection of the DOM, seemed to see value=0..n on the options, but obviously that was incorrect!

The final incantation which was a thing of beauty (thanks to your help!) was:

ng-options="day as (day | ordinal) for day in monthdays"




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 



--
Eric Hahn, Inventures Group 
er...@ingroup.comwww.ingroup.com 
Tel +1 650 326 2990, Fax +1 650 326 2988

Prabir Mohanty

unread,
May 29, 2014, 1:48:02 AM5/29/14
to ang...@googlegroups.com
Hi, I have same problem. You got any solution for array binding?

kristijan budimir

unread,
May 30, 2014, 2:46:32 AM5/30/14
to ang...@googlegroups.com
Hy, don't be embarrassed by this. Select is one very irritating thing in angularjs. Try this one, it worked for me

Only thing here a had to set "options" as array with objects with propertis name, value and selected
var options = [{name:"name1", value="value1", selected=false},
    {name:"name2", value="value2", selected=true},
    {name:"name3", value="value3", selected=false},]

<select class="form-control" ng-model="modelName">
                <option ng-repeat="option in options" value={{option.value}} data-ng-selected="{{option.selected}}">{{option.name}}</option>
            </select>
Reply all
Reply to author
Forward
0 new messages