How to implement angular 7 drop-down with images?

2019-08-19 08:54发布

I'm creating angular 7 app, and using mat-select for drop-down. Now i want add image within each item of the mat-select

See the image below:

enter image description here

1条回答
来,给爷笑一个
2楼-- · 2019-08-19 09:18

Ensure you have images in the array which you're providing in the mat-option; then, create an image tag and provide the image source to it.

relevant 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:

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

查看更多
登录 后发表回答