如何使用图像实现角度7下拉菜单?

时间:2019-07-12 08:27:11

标签: html css typescript angular-material angular7

我正在创建angular 7应用,并使用mat-select进行下拉。现在,我想在mat-select

的每个项目中添加图片

请参见下图:

enter image description here

1 个答案:

答案 0 :(得分:1)

确保在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