Search code examples
angularjsng-options

AngularJS how to add data-image attribute to option


I want to add custom data attribute to option tag.

For example:

<select>
    <option data-image="url 1">Val 1</option>
    <option data-image="url 2">Val 2</option>
    <option data-image=" ... "> ... </option>
    <option data-image="url N">Val N</option>
<select>

How can I do that?


Solution

  • That is impossible with the select directive (see the documentation). But you can easily make what you want with ngRepeat (see the documentation):

    <select ng-model="choice">
        <option ng-repeat="item in itemsList" data-image="{{item.url}}">{{item.label}}</option>
    </select>
    

    JSFiddle