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>
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>
Share
Improve this question
edited Apr 10, 2013 at 2:09
rdodev
asked Apr 10, 2013 at 1:54
rdodevrdodev
3,2023 gold badges28 silver badges34 bronze badges
3 Answers
Reset to default 5I 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/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 .)
is it not
<select ... ng-options="(key, value) in objs" ...>
This should be used like this:
<select>
<option ng-repeat="objin objs" value="{{obj.key}}">{{obj.value}}</option>
</select>