0
votes

I'm facing an issue that only occurs on Chrome. This concerns a component using matAutocomplete in a *ngFor loop. Options panel is not displayed when focusing into the binded input. In order to make sure that it was not only related to my code, I performed a very simple test by replacing all the code of my component with the one provided by the example below:

import {Component, OnInit} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';

export interface User {
  name: string;
}

/**
 * @title Display value autocomplete
 */
@Component({
  selector: 'autocomplete-display-example',
  templateUrl: 'autocomplete-display-example.html',
  styleUrls: ['autocomplete-display-example.css'],
})
export class AutocompleteDisplayExample implements OnInit {
  myControl = new FormControl();
  options: User[] = [
    {name: 'Mary'},
    {name: 'Shelley'},
    {name: 'Igor'}
  ];
  filteredOptions: Observable<User[]>;

  ngOnInit() {
    this.filteredOptions = this.myControl.valueChanges
      .pipe(
        startWith(''),
        map(value => typeof value === 'string' ? value : value.name),
        map(name => name ? this._filter(name) : this.options.slice())
      );
  }

  displayFn(user?: User): string | undefined {
    return user ? user.name : undefined;
  }

  private _filter(name: string): User[] {
    const filterValue = name.toLowerCase();

    return this.options.filter(option => option.name.toLowerCase().indexOf(filterValue) === 0);
  }
}

And

<form class="example-form">
  <mat-form-field class="example-full-width">
    <input type="text" placeholder="Assignee" aria-label="Assignee" matInput [formControl]="myControl" [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
      <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
        {{option.name}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

Then, component consumer is a step of a mat-step belonging to a mat-horizontal-stepper:

...
<mat-step label="Collège">
   <autocomplete-display-example></autocomplete-display-example> (1)
   <ng-container *ngFor="let item of myarray">
      <autocomplete-display-example></autocomplete-display-example> (2)
   </ng-container>
</mat-step>
...

Line (1) works perfectly. But with (2), when focusing into the input, the option panel is not displayed. The only way to get it is to click on the mat-step label. But anyway, I'm not able to select an option.

This behaviour only occurs on Chrome, not Edge.

I will apreciate some help.

1

1 Answers

0
votes

A very ugly solution consists on (thx to https://www.gistia.com/improve-angular-performance-efficiency) detaching change detector and performing detect changes on a time elapse interval.

....

constructor(pCdr: ChangeDetectorRef) {}

....

//entering the step
this.pCdr.detach()
this.mCdrInterval = setInterval(() => {
this.mCdr.detectChanges();
}, 500);

....

//leaving the step
clearInterval(this.mCdrInterval);
this.mCdr.reattach();

But, I'm still expecting a more tidy approach...