Can't get select working with ng-options

303 views
Skip to first unread message

Pascal Precht

unread,
Jan 8, 2013, 6:48:13 AM1/8/13
to ang...@googlegroups.com
Hey guys,

I know, this topic is discussed a lot in here, and there's also a documentation about select and the ng-options directive, but I still can't get a simple select working with my data.

The goal:

<select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

Nothing serious. My data looks like this:

$scope.countOptions = {
    "1":"1",
    "2":"2",
    "3":"3"
};

So how does the ng-options attribute has to look like?

<select ng-model="count" ng-options="label for (key, value) in countOptions"></select>

Doesn't work.

Please help me out!

m48u

unread,
Jan 8, 2013, 7:32:57 AM1/8/13
to ang...@googlegroups.com
Hi!

For your data you should choose not a JSON object but an array to iterate over. 
Next, in your array you should have similiar objects (every objaect has the same keys)

Here is a working jsFiddle: http://jsfiddle.net/m48u/ZLgAf/

$scope.countOptions = [
   {option: "1"},
   {option: "2"},
   .....
];

<select ng-model="model.selection" ng-options="co.option for co in countOptions"></select>

Peter Bacon Darwin

unread,
Jan 8, 2013, 7:34:38 AM1/8/13
to ang...@googlegroups.com
<select ng-model="count" ng-options="value as key for (key, value) in countOptions"></select>


--
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-US.
 
 

m48u

unread,
Jan 8, 2013, 7:35:24 AM1/8/13
to ang...@googlegroups.com
here an other update to show the selected item: http://jsfiddle.net/m48u/ZLgAf/1/

Pascal Precht

unread,
Jan 8, 2013, 7:39:28 AM1/8/13
to ang...@googlegroups.com
Thanks Pete, but your snippet gives me:

TypeError: Object #<Object> has no method 'prop'

Peter Bacon Darwin

unread,
Jan 8, 2013, 7:39:58 AM1/8/13
to ang...@googlegroups.com
And here is one using the original (admittedly weird) object for options: http://jsfiddle.net/BMGCe/


--

Pascal Precht

unread,
Jan 8, 2013, 7:46:30 AM1/8/13
to ang...@googlegroups.com
Thanks a lot! It's more weird that the same code doesn't work for me, but nevertheless thanks for the working example.

Pascal Precht

unread,
Jan 8, 2013, 8:03:07 AM1/8/13
to ang...@googlegroups.com
Found the problem. In my project's environment there's an included jQuery in version 1.4.1. When removing it, no error occurs. (And please don't ask why it's 1.4.1)


Am Dienstag, 8. Januar 2013 13:39:58 UTC+1 schrieb Peter Bacon Darwin:
Reply all
Reply to author
Forward
0 new messages