ng-options with array of objects?

2,737 views
Skip to first unread message

Stephen Friedrich

unread,
Jan 21, 2014, 9:58:57 AM1/21/14
to ang...@googlegroups.com
I cannot get ng-options to work:

My model that I'd like to update via a drop down is
wifiSettings = { channelNumber: 2, ... }

and the available options should come from an array like
availableChannels = [
    {
        number: 1,
        frequency: 2412,
        maxTransmitPower: 20
    },
    {
        number: 2,
        frequency: 2417,
        maxTransmitPower: 20
    },
    ....
 ]

I tried with several expressions in ng-options, but nothing seemed to work, for example, this does never show the selected value:
        <select id="channel"
                ng-model="wifiSettings.channelNumber"
                ng-options="channel.number as channel.number for channel in availableChannels track by channel.number">
        </select>

(Ideally the labels in the options should be an expression like "{{ channel.number + '(' + channel.frequency + 'MHz)' }}" but that's another story.)

Here is a (broken) example: http://plnkr.co/edit/4e28Pe?p=info

Sander Elias

unread,
Jan 21, 2014, 10:55:48 AM1/21/14
to ang...@googlegroups.com
Hi Stephen,

Have a look at this plunk. The Track by was throwing the ng-options off. It isn't exactly the same as ng-repeat ;)

Regards
Sander

Stephen Friedrich

unread,
Jan 23, 2014, 3:09:28 AM1/23/14
to ang...@googlegroups.com
Works great - thanks a million!

I still wonder what to make of the description of "track by" in the "select ng-options" docs:
http://code.angularjs.org/1.2.9/docs/api/ng.directive:select
"Used when working with an array of objects. The result of this expression will be used to identify the objects in the array."
Reply all
Reply to author
Forward
0 new messages