简体   繁体   English

Angular材料; mat-select 显示选择的颜色

[英]Angular material; mat-select display selected color

I have this piece of code to display list of colors that could be chosen by user:我有这段代码可以显示用户可以选择的 colors 列表:

<form>
    <h4>mat-select</h4>
    <mat-form-field appearance="fill">
        <mat-label>Favorite Color</mat-label>
        <mat-select [(ngModel)]="selectedValue" name="food">
            <mat-option *ngFor="let color of allColors" [value]="'#' + color.value">
                <!-- {{color.label}}  -->
                <span class="color-span" [ngStyle]="{ 'background-color': '#' + color.value }"></span>
            </mat-option>
        </mat-select>
    </mat-form-field>

</form>

import {Component} from '@angular/core';
/**
 * @title Select in a form
 */
@Component({
  selector: 'select-form-example',
  templateUrl: 'select-form-example.html',
})
export class SelectFormExample {
  public allColors: any[] = [
  {label: 'FFFFFF', value: 'FFFFFF'},
  {label: '000000', value: '000000'},
  {label: '603813', value: '603813'},
  {label: 'FF0000', value: 'FF0000'},
  {label: '2E3192', value: '2E3192'},

  {label: '006837', value: 'FFD400'},
  {label: 'F15A24', value: 'F15A24'},
  {label: 'CCCCCC', value: 'CCCCCC'},
  {label: 'DBC0B5', value: 'DBC0B5'},
  {label: 'FAB49B', value: 'FAB49B'},

  {label: '87B2C7', value: '87B2C7'},
  {label: 'ACD58A', value: 'ACD58A'},
  {label: 'FFF9AE', value: 'FFF9AE'}
];
}

It works as you see in the image below.它的工作原理如下图所示。 But I don't know how to display selected color in the mat-select control.但我不知道如何在 mat-select 控件中显示选定的颜色。

在此处输入图像描述

Demo演示

If you want to customized the selected value their is a selector which you can use mat-select-trigger .如果您想自定义选定的值,它们是一个选择器,您可以使用mat-select-trigger It allows you to customize the trigger that is displayed when the select has a value.它允许您自定义当 select 有值时显示的触发器。

Reference:- https://material.angular.io/components/select/api参考:- https://material.angular.io/components/select/api

You can modify your code like below to show the color when user select the value.您可以像下面这样修改代码以在用户 select 值时显示颜色。

<mat-select [(ngModel)]="selectedValue" name="food">
    <mat-select-trigger>
        <span [ngStyle]="{ 'background-color': selectedValue }"/>
    </mat-select-trigger>
    <mat-option *ngFor="let color of allColors" [value]="'#' + color.value">
        <!-- {{color.label}}  -->
        <span class="color-span" [ngStyle]="{ 'background-color': '#' + color.value }"/>
    </mat-option>
</mat-select>

Demo 演示

mat-select sets the value and displays the label of the mat-option you select, however, it will only show the text from that selection, since you are not showing any text in your labels, nothing is displayed when you select one of the options. mat-select设置值并显示 select 的mat-option的 label,但是,它只会显示来自该选择的文本,因为您没有在标签中显示任何文本,所以当您使用 Z99938282F0407184592ZEF16选项。

You can add the label for your color in the mat-option elements and then, once you select something, that text will appear in the mat-select element.您可以在mat-option元素中为您的颜色添加 label,然后,一旦您使用 select 某些内容,该文本将出现在mat-select元素中。 If you want a visual representation of the color - you can show the value outside of the mat-select element (maybe to the right?).如果您想要颜色的视觉表示 - 您可以在 mat-select 元素之外显示值(可能在右边?)。

You can also consider changing the label on your array from the hex value to the name of the color - eg {label: 'FFFFFF', value: 'FFFFFF'}, to {label: 'White', value: 'FFFFFF'},您还可以考虑将阵列上的 label 从十六进制值更改为颜色的名称 - 例如{label: 'FFFFFF', value: 'FFFFFF'},{label: 'White', value: 'FFFFFF'},

Here is a snippet which shows a suggestion:这是一个显示建议的片段:

<mat-select [(ngModel)]="selectedValue" name="food">
    <mat-option *ngFor="let color of allColors" [value]="'#' + color.value">
        <!-- {{color.label}}  -->
        <span class="color-span" [ngStyle]="{ 'background-color': '#' + color.value }"></span>
        {{ color.label }}
    </mat-option>
</mat-select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM