如何在Ionic 2中的选择组件内使用图像

Joh*_*ohn 9 ionic2 ionic2-select

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

<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>
Run Code Online (Sandbox Code Playgroud)

有谁有想法吗?

Ket*_*ale 3

ion-select 组件不允许对其自身进行直接自定义,并且您添加到 ion-select 和 ion-option 中的任何不符合 ionic 文档的内容都将在生成的输出中被忽略。

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

一种方法是将 ion-select 放入带有 class 的父元素(如 div 或 ion-row 等)中,并使用.parentclass childElement选择器应用 CSS 规则。

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

    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);
}
Run Code Online (Sandbox Code Playgroud)

我已经使用离子选择实现了颜色和图像选择器。请参考https://github.com/ketanyekale/ionic-color-and-image-selector

您还可以在Ionic 选择颜色输入中检查答案