简体   繁体   中英

Angular ng-repeat in dropdown

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?

You 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.com/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM