根据 selected 值更改 ion-select 的背景颜色
Change the background color on ion-select based on the selected value
我正在使用 Ionic 5.0,并且我有一个 ion-select 场。我需要根据 selected 选项更改字段的背景。
我已经尝试了几种方法。我目前在下面的代码中尝试在 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>
还有我的老师
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)
<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,
};
}
呜呜呜!有用!非常感谢阿伦!
这是包含离子内容的最终 HTML 代码...
<ion-select [(ngModel)]="selectedVal" [ngStyle]="styleObj" (ionChange)="SetBackground($event)" class="square" interface="popover">
<ion-select-option *ngFor="let item of data" [value]="item.id" >
{{ item.name }}
</ion-select-option>
</ion-select>
还有 ts...
styleObj = {};
SetBackground(bg) {
this.styleObj = {
'background-color': bg.target.value,
};
}
再次感谢!
ps。这是下一步...
我想要在屏幕上显示这些 select 语句的 11x11 网格,并且每个语句都有独立的颜色。你会怎么做?
我正在使用 Ionic 5.0,并且我有一个 ion-select 场。我需要根据 selected 选项更改字段的背景。
我已经尝试了几种方法。我目前在下面的代码中尝试在 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>
还有我的老师
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)
<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,
};
}
呜呜呜!有用!非常感谢阿伦!
这是包含离子内容的最终 HTML 代码...
<ion-select [(ngModel)]="selectedVal" [ngStyle]="styleObj" (ionChange)="SetBackground($event)" class="square" interface="popover">
<ion-select-option *ngFor="let item of data" [value]="item.id" >
{{ item.name }}
</ion-select-option>
</ion-select>
还有 ts...
styleObj = {};
SetBackground(bg) {
this.styleObj = {
'background-color': bg.target.value,
};
}
再次感谢!
ps。这是下一步...
我想要在屏幕上显示这些 select 语句的 11x11 网格,并且每个语句都有独立的颜色。你会怎么做?