简体   繁体   English

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

[英]How to implement angular 7 drop-down with images?

I'm creating angular 7 app, and using mat-select for drop-down.我正在创建 angular 7 应用程序,并使用 mat-select 进行下拉。 Now i want add image within each item of the mat-select现在我想在mat-select每个项目中添加图像

See the image below:见下图:

在此处输入图片说明

Ensure you have images in the array which you're providing in the mat-option ;确保您在mat-option提供的数组中有图像; then, create an image tag and provide the image source to it.然后,创建一个图像标签并为其提供图像源。

relevant HTML :相关的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>

relevant TS :相关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' }
  ];
}

complete working stackblitz here在这里完成工作堆栈闪电战

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

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