N
N
Nikolay Semenov2018-08-12 10:14:01
JavaScript
Nikolay Semenov, 2018-08-12 10:14:01

How not to render the list if it is empty?

Guys, hello everyone!
there is such a code

<mat-form-field class="example-full-width">
        <input type="text" matInput placeholder="serch" [matAutocomplete]="auto" [formControl]="searchStr">
        <mat-autocomplete #auto="matAutocomplete" showPanel="false" isOpen="false">
          <mat-option (onSelectionChange)="selectedFilm(film)" *ngFor="let film of films.results" [value]="film.title">
            <img class="example-option-img" aria-hidden [src]="getUrlImage(film.poster_path)" height="25">
            <span>{{film.title}}</span>
          </mat-option>
        </mat-autocomplete>
      </mat-form-field>

it's autocomplete from material Angular
I put showPanel="false" isOpen="false" and it still throws an error: Cannot read property 'results' of null
How can anyone come across?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-08-12
@nickola105

showPanel="false" isOpen="false"
will not affect anything, because ngFor will still be rendered. These options only define behavior.
it's all right?

<mat-form-field class="example-full-width">
        <input type="text" matInput placeholder="serch" [matAutocomplete]="auto" [formControl]="searchStr">
        <mat-autocomplete #auto="matAutocomplete" showPanel="false" isOpen="false" *ngIf="films?.results?.length">
          <mat-option (onSelectionChange)="selectedFilm(film)" *ngFor="let film of films.results" [value]="film.title">
            <img class="example-option-img" aria-hidden [src]="getUrlImage(film.poster_path)" height="25">
            <span>{{film.title}}</span>
          </mat-option>
        </mat-autocomplete>
      </mat-form-field>

But in general it is good to initialize the initial value somehow. To be an empty array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question