Angular Material 2 - Custom mat template / md-option in autocomplete

Does Angular Material 2 have a selector for setting autocomplete options such as Angular Material md-item-templatefor AngularJS? I could not find anything on the docs from Material 2

AngularJS example from docs :

<md-autocomplete
    <md-item-template>
      <span class="item-title">
        <md-icon md-svg-icon="img/icons/octicon-repo.svg"></md-icon>
        <span> {{item.name}} </span>
      </span>
      <span class="item-metadata">
        <span class="item-metastat">
          <strong>{{item.watchers}}</strong> watchers
        </span>
        <span class="item-metastat">
          <strong>{{item.forks}}</strong> forks
        </span>
      </span>
    </md-item-template>
</md-autocomplete>

enter image description here

+4
source share
2 answers

I am also looking for the right way to do this, as good stuff. This is an ugly hack, I can make it work in Material 5:

Style:

.mat-option {
  height: 50px;
  line-height: 20px;
}

Template:

<mat-autocomplete #auto="matAutocomplete">
    <mat-option *ngFor="let state of filteredStates | async" [value]="state.name">
    <!-- <img style="vertical-align:middle;" aria-hidden src="{{state.flag}}" height="25" /> -->
        <span>{{ state.name }}</span><br>
        <small>Population: {{state.population}}</small>         
    </mat-option>
</mat-autocomplete>

Try here

+1
source

Two-line matte image auto-complete solution with image

Stackblitz example

<form class="example-form">
  <mat-form-field class="example-full-width">
    <input matInput placeholder="State" aria-label="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let state of filteredStates | async" [value]="state.name">
        <img style="vertical-align:middle;" aria-hidden src="{{state.flag}}" height="20" />
        <span>{{ state.name }}</span> <br>
        <small>Population: {{state.population}}</small>
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>
// CSS
.mat-option {
  height: 50px;
  line-height: 20px;
}
0
source

Source: https://habr.com/ru/post/1686767/


All Articles