如何在Ionic 2中的选择组件内使用图像 [英] How to use images inside a select component in Ionic 2

查看:107
本文介绍了如何在Ionic 2中的选择组件内使用图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像放入 在Ionic 2中选择 组件:
我已将图像源文件放入我的Ionic中的 www / img 文件夹中2个项目。
但是,使用简单的 img -tag不会使用此代码显示任何图像:

I am trying to put an image inside a Select component in Ionic 2 : I have put the image source files inside the www/img folder in my Ionic 2 project. However, using a simple img-tag does not display any image using this code:

<ion-list>
  <ion-item>
    <ion-label>Gaming</ion-label>
    <ion-select [(ngModel)]="gaming">
      <ion-option value="nes">
        NES
        <img src="img/myImage.png">
      </ion-option>
    </ion-select>
  </ion-item>
</ion-list>

有没有人有任何想法?

推荐答案

离子选择组件不允许直接定制到自身,并且您添加到离子选择和离子选项中的任何内容(不是根据离子文档)将在生成的输出。

The ion-select component doesn't allow direct customization to itself and anything that you add to ion-select and ion-option which is not as per the ionic documentation, will be ignore in the generated output.

您无法在组件中添加类或样式。

You cannot add class or style to the component.

执行此操作的一种方法是将ion-select放入父元素(如div或ion-row等)中,并使用 .parentclass childElement selector来应用CSS规则。

One way to do this is to put the ion-select in a parent element like div or ion-row etc. with class and apply the CSS rules using .parentclass childElement selector.

要显示选项中的图像,请检查以下功能:

To show the images in option check the function below:

    prepareImageSelector() {
    setTimeout(() => {
        let buttonElements = document.querySelectorAll('div.alert-radio-group button');
        if (!buttonElements.length) {
            this.prepareImageSelector();
        } else {
            for (let index = 0; index < buttonElements.length; index++) {
                let buttonElement = buttonElements[index];
                let optionLabelElement = buttonElement.querySelector('.alert-radio-label');
                let image = optionLabelElement.innerHTML.trim();
                buttonElement.classList.add('imageselect', 'image_' + image);
                if (image == this.image) {
                    buttonElement.classList.add('imageselected');
                }
            }
        }
    }, 100);
}

我使用ion-select实现了颜色和图像选择器。请参考 https://github.com/ketanyekale/ionic-color-and-图像选择器

I have implemented color and image selector using ion-select. Please refer https://github.com/ketanyekale/ionic-color-and-image-selector

您还可以在离子选择颜色输入

这篇关于如何在Ionic 2中的选择组件内使用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆