根据所选值更改离子选择的背景颜色

耶夫

我正在使用Ionic 5.0,并且有一个离子选择字段。我需要根据所选选项更改字段的背景。

我已经尝试了几件事。我目前在以下代码中的尝试是在ion-select中插入[ngStyle] =“ SetBackground(item.id)”,但这不起作用。我也尝试过[ngStyle] =“ SetBackground($ event)”。我曾想过以某种方式将其添加到(ionChange)=“ OnChange($ event)”中,但不知道该怎么做。似乎我只需要使用item.id即可更改背景,但是对于我的一生,我只是想不通。

这是我的HTML代码。我希望这足以帮助我找到答案。

<ion-content>

  <div id="game-board">

    <ion-img class="map-img" src="../assets/img/map-a.png"></ion-img>

    <ion-select [(ngModel)]="selectedVal" class="square" interface="popover" [ngStyle]="SetBackground(item.id)">

      <ion-select-option *ngFor="let item of data" [value]="item.id" >

        {{ item.name }}

      </ion-select-option>

    </ion-select>

  </div>

</ion-content>

和我的ts

import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  data: { id: string; name: string; }[];

  constructor(private platform: Platform) {
    this.platform.ready().then(() => {
      this.data = [
        {
          id: "transparent", name: ""
        },
        {
          id: "green", name: "Forest"
        },
        {
          id: "red", name: "Village"
        },
        {
          id: "yellow", name: "Field"
        },
        {
          id: "blue", name: "Water"
        },
        {
          id: "purple", name: "Monster"
        },
      ]
    })
  }

  SetBackground(bg) {
    let newColor = {
      'background-color': bg.target.value,
    };
    return newColor
  }

我得到的错误...

HomePage.html:12 ERROR TypeError: Cannot read property 'id' of undefined
    at Object.eval [as updateDirectives] (HomePage.html:14)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:45259)
    at checkAndUpdateView (core.js:44271)
    at callViewAction (core.js:44637)
    at execComponentViewsAction (core.js:44565)
    at checkAndUpdateView (core.js:44278)
    at callViewAction (core.js:44637)
    at execEmbeddedViewsAction (core.js:44594)
    at checkAndUpdateView (core.js:44272)
    at callViewAction (core.js:44637)
阿伦·莫汉(Arun Mohan)
<ion-select [(ngModel)]="selectedVal" class="square" interface="popover" [ngStyle]="SetBackground(item.id)">

  <ion-select-option *ngFor="let item of data" [value]="item.id" >

您的SetBackground函数在循环之前被调用。因此,没有item.id的参考。

你可以这样。

html

<select [ngStyle]="styleObj" (change)="SetBackground($event)">

  <option *ngFor="let item of data"  value={{item.id}}>

    {{ item.name }}

  </option>

</select>

ts

 data = [
        {
          id: "transparent", name: ""
        },
        {
          id: "green", name: "Forest"
        },
        {
          id: "red", name: "Village"
        },
        {
          id: "yellow", name: "Field"
        },
        {
          id: "blue", name: "Water"
        },
        {
          id: "purple", name: "Monster"
        },
      ];

  styleObj = {};

  SetBackground(e) {
    this.styleObj = {
      'background-color': e.target.value,
    };
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据所选选项动态更改背景颜色选择元素

来自分类Dev

如何根据所选选项的值更改选择的选项?

来自分类Dev

jQuery根据所选值更改选择文本

来自分类Dev

根据第3 td中的值更改背景颜色

来自分类Dev

根据值更改Excel工作表背景颜色

来自分类Dev

根据单元格值更改行背景颜色

来自分类Dev

选择的插件:根据其值更改选项的背景

来自分类Dev

根据所选值更改列宽

来自分类Dev

uwp:如何根据其值更改listview项目的背景颜色?

来自分类Dev

如何根据表格单元格中的值更改表格行的背景颜色?

来自分类Dev

django-table2-根据特定列的值更改整个行的背景颜色

来自分类Dev

如何使用JavaScript / jQuery根据表中的值更改td背景颜色?

来自分类Dev

EXCEL VBA根据值更改背景颜色-RGB不起作用

来自分类Dev

根据 MS Access 数据库中的值更改按钮背景颜色

来自分类Dev

尝试根据多单元格值更改数据网格中行的背景颜色

来自分类Dev

如何通过VBA根据其他单元格值更改单元格的背景颜色

来自分类Dev

根据下拉列表值更改 HTML 表格单元格的背景颜色?

来自分类Dev

使用输入值更改背景颜色

来自分类Dev

根据布尔值更改边框颜色

来自分类Dev

Fullcalendar-根据值更改事件颜色

来自分类Dev

如何根据值更改<td>元素的颜色

来自分类Dev

根据字符串值更改颜色

来自分类Dev

根据搜寻条值更改颜色

来自分类Dev

根据布尔值更改边框颜色

来自分类Dev

根据被过滤的值更改字体颜色angularjs

来自分类Dev

根据列值更改行的颜色

来自分类Dev

根据值更改行颜色(行内)

来自分类Dev

根据值更改跨度标记的颜色

来自分类Dev

根据 td 的值更改 Td 的颜色

Related 相关文章

热门标签

归档