Angular select with images

All we need is an easy explanation of the problem, so here it is.

THE SITUATION:

I need to insert flags inside the language select. I have searched in Google and StackOverflow but the solutions founded are not working for me.

THE CODE:

In the controller:

 $scope.language_list = [{'name': 'english', 'url': 'https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/gb.png'},{'name': 'italian', 'url': 'https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/it.png'}];

In the view:

<select ng-model="language_selected">
    <option ng-repeat="language in language_list" data-image="{{language.url}}" >{{language.name}}</option>
</select>

EXAMPLE:

http://jsfiddle.net/tcVhN/168/

THE QUESTION:

How can i insert images inside the angular select?

EDIT:

Here is the Plunker of the solution, cleaned up:

http://plnkr.co/edit/hMlNjkDL3l0QKF37pCa0?p=preview

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

As other answers you can not use <options> to add an image, however you can use angular module ui-select to achieve what you want to do.

Here is a demo with ui-select with your data.

Clean up the code and get what you need.

If you’re not interested in search box override the CSS as,

 .select2-search {
      display: none;
 }

DEMO

Method 2

this is impossible with a native select element,

therfore, you need to design a selectbox using other html elements & css,

try this out:

var app = angular.module('app',[]);

//app.directive('appDirective', function() {});
//app.factory('appService', function() {});

function AppCtrl($scope) {
	$scope.obj={language_selected : {'name':'Choose a language'}};
    $scope.language_list = [{'name': 'english', 'url': 'https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/gb.png'},{'name': 'italian', 'url': 'https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/it.png'}];
    
}
.select_list{
    background-color: #fff;
    border: 1px solid #b3b3b3;
    cursor: pointer;
    height: 21px;
    line-height: 21px;
    padding: 3px 5px;
    position: relative;
    vertical-align: middle;
    width: 250px;
}
.select_list:after{
    content:"▼";
    position:absolute;
    right:3px;
    color:#b3b3b3;
}
.select_list > .options{
    display:none;
    position:absolute;
    top:100%;
    left:-1px;
    width:100%;
    border:1px solid #666;
    padding:0;
    margin:0;
}

.select_list.active > .options{
    display:block;
}

.select_list > span, .select_list > .options li {
    background-position: 5px center;
    background-repeat: no-repeat;
    padding-left: 30px;
    list-style:none;
}

.select_list > .options li:hover {
    background-color:#eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<div ng-app="app"  ng-controller="AppCtrl">
<div class="select_list" ng-class='{active:active}' ng-click="active=!active">
    <span ng-style="{'background-image':'url('+obj.language_selected.url+')'}">{{obj.language_selected.name}}</span>
    <ul class="options">
        <li class="select_list_option" ng-repeat="language in language_list" ng-click="obj.language_selected=language" ng-style="{'background-image':'url('+language.url+')'}">{{language.name}}</li>
        </ul>
</div>
</div>

Method 3

The Answer:

Its simply not possible.

The permitted content of a <option> tag is Text with eventually escaped characters (like &eacute;).
HTML isnt allowed.

See the docs for more information.

The solution:

Make a custom Dropdown with CSS and HTML.

Method 4

It’s not possible, as <option> only supports text.

You may have to roll your own drop-down control using complex HTML/CSS/JavaScript. How to do it may or may not be within the scope of your question.

Alternatively, you may use a non-repeating background-image and apply some padding on the text to achieve a similar effect. But if each <option> is to have a unique image, your code is going to be polluted with a style attribute for every single <option>. If you don’t mind that, it’s fine. Otherwise, roll your own somehow.

Referred from : Adding images with option tag

Method 5

Instead of inserting images you could generate css classes (from the language name or adding a new value only for that) and add the images as background images in your css (using sprites).

<select ng-model="language_selected">
    <option ng-repeat="language in language_list" ng-class="{{ language.name }}" >{{ language.name }}</option>
</select>

For the css sprites I usually use sass with compass.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply