最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular ng-repeat in dropdown - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论