html - 如何使用图像实现 angular 7 下拉菜单?

标签 html css angular angular-material angular7

我正在创建 angular 7 应用程序,并使用 mat-select 进行下拉。现在我想在 mat-select 的每个元素中添加图像

见下图:

enter image description here

最佳答案

确保您在 mat-option 中提供的阵列中有图像。 ;然后,创建一个图像标签并为其提供图像源。

相关 HTML :

<h4>Basic mat-select with images</h4>
<mat-form-field>
  <mat-label>Favorite food</mat-label>
  <mat-select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      <img src='{{food.img}}'> {{food.viewValue}}
    </mat-option>
  </mat-select>
</mat-form-field>

相关 TS :
import { Component } from '@angular/core';

export interface Food {
  value: string;
  viewValue: string;
  img: string;
}

@Component({
  selector: 'select-overview-example',
  templateUrl: 'select-overview-example.html',
  styleUrls: ['select-overview-example.css'],
})
export class SelectOverviewExample {
  foods: Food[] = [
    { value: 'steak-0', viewValue: 'Steak', img: 'https://www.akberiqbal.com/favicon-32x32.png' },
    { value: 'pizza-1', viewValue: 'Pizza', img: 'https://www.akberiqbal.com/favicon-16x16.png' },
    { value: 'tacos-2', viewValue: 'Tacos', img: 'https://www.akberiqbal.com/favicon-96x96.png' }
  ];
}

完整 working stackblitz here

关于html - 如何使用图像实现 angular 7 下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57003063/

相关文章:

javascript - 输入返回的可观察值

javascript - 如何从html元素获取数组索引?

javascript - 在一个点停止一个位置固定的div

javascript - 按钮不响应 chrome/safari 中的 onclick,但在堆栈溢出时处理代码片段,为什么?

javascript - 即使短语确实存在于数组中,正则表达式搜索也会返回 -1

javascript - Angular 2 中的多选下拉列表

html - Raphael js文本定位: centering text in a circle

html - 如何使用 Go 修改 HTML 文件的元素?

html - 将标题文本放在导航栏旁边

javascript - YouTube 网格图库 : How can I get the player to appear in a popup?