I'm trying to do this with ng-repeat
in Angular:
<select ng-model="selected_item">
<option ng-repeat="item in items" value="{{item.id}}"
ng-change="doSomething(selected_item)">
{{item.name}} - {{item.another_attribute}}
</option>
</select>
and getting the error No controller: ngModel
.
Usually I construct dropdowns with ng-options
but since the label of the options are made up of two different attributes, it doesn't work.
<select ng-model="selected_item"
ng-options="item.id as item.name for item in items"
ng-change="doSomething(selected_item)">
As you can see I cannot use two attributes here for the label.
Any ideas?
I'm trying to do this with ng-repeat
in Angular:
<select ng-model="selected_item">
<option ng-repeat="item in items" value="{{item.id}}"
ng-change="doSomething(selected_item)">
{{item.name}} - {{item.another_attribute}}
</option>
</select>
and getting the error No controller: ngModel
.
Usually I construct dropdowns with ng-options
but since the label of the options are made up of two different attributes, it doesn't work.
<select ng-model="selected_item"
ng-options="item.id as item.name for item in items"
ng-change="doSomething(selected_item)">
As you can see I cannot use two attributes here for the label.
Any ideas?
Share Improve this question asked Sep 18, 2013 at 20:33 adamorsadamors 2,6565 gold badges33 silver badges46 bronze badges 1- you not injected your ngModel in app – Nitish Kumar Commented Sep 18, 2013 at 20:37
1 Answer
Reset to default 8You can concatenate strings in the label expression. So
ng-options="item.id as (item.name + '-' + item.another_attribute) for item in items"
should work just fine.
quick demo: http://jsbin./OKoviMA/1/
To be more precise, the label expression is a real angular expression, so you could for example also apply a filter or call a controller method, even passing the current itteration value as a parameter.