rdodev
rdodev

Reputation: 3192

AngularJS DropDown with ng-options

Let's say I have a Controller like such:

function MockController($scope) {
$scope.objs = [{"a": "true"},{"b": "false"}];
$scope.Value = "";
}

In the html view I'd have something like:

  <select class="input-block-level" ng-model="Value" 
  ng-options="(obj.key, obj.value) for obj in objs" required>

However, no matter how I try, angular doesn't seem to like the tuple notation. I tried without parenths and no dice. Is there a generic way to treat hashtables/dictionaries in ng-repeat? That is, assume you don't know the name of the key and they key itself should be used like such:

<option value={{obj.key}}>{{obj.value}}</option>

Upvotes: 3

Views: 6633

Answers (3)

user2107373
user2107373

Reputation: 437

This should be used like this:

<select>
<option ng-repeat="objin objs" value="{{obj.key}}">{{obj.value}}</option>
</select>

Upvotes: 1

MJV
MJV

Reputation: 1882

I don't think it's possible to refer to unknown keys in ng-options.

I'd probably try to write a wrapper for the data somewhere along the way, converting it into something like:

[{"key": "a", "value": "true"},
 {"key": "b", "value": "false"}]

(The problem was so interesting that I actually wrote one already: http://jsfiddle.net/RCU8M/ - but that won't work in browsers without ECMAScript5 support, so you might want to fix that by using a more elegant solution, like in get keys of json-object in JavaScript .)

Upvotes: 5

akonsu
akonsu

Reputation: 29538

is it not

<select ... ng-options="(key, value) in objs" ...>

Upvotes: 2

Related Questions