Angular select with images

2020-03-30 04:18发布

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

5条回答
够拽才男人
2楼-- · 2020-03-30 04:30

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

查看更多
何必那么认真
3楼-- · 2020-03-30 04:34

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.

查看更多
我只想做你的唯一
4楼-- · 2020-03-30 04:40

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.

查看更多
5楼-- · 2020-03-30 04:41

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

查看更多
趁早两清
6楼-- · 2020-03-30 04:51

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>

查看更多
登录 后发表回答