3

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>

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

is it not

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

1 Comment

Let me try this. I"ll report back.
1

This should be used like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.