Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Angular 8 mat-autocomplete

when I focus on the Input field then dropdown first value gets highlight. When I press the tab button then highlighted the value set in the input field. when I click outside the Input field then highlighted value should not set in the input field But it is getting set How to fix this?

Code:

<input mat-tab-directive [style.color]="isValueModified()" (focus)="focusFunction()"
    type="text" [formControl]="myControl" [matAutocomplete]="auto"
    (onMatTabSelectionChange)="onMatTabSelectionChange($event)" (blur)="inputBlur($event)">
<mat-autocomplete #auto="matAutocomplete" [autoActiveFirstOption]="true">
<mat-option *ngFor="let item of filteredOptions | async; [value]="item.value">
    {{item.description}}
</mat-option>
</mat-autocomplete>

myControl: FormControl = new FormControl();
filteredOptions: Observable<any[]>;

focusFunction() {
    this.filteredOptions = this.myControl.valueChanges
      .pipe(
        startWith(''),
        map(value => this.filter(value))
      );
  }

onMatTabSelectionChange(event) {
    var val = event.value;
    if (event.active) {
      this.myControl.setValue(val);
    }
}

inputBlur(event) {
    
}


import { Directive, AfterViewInit, OnDestroy, Optional, Output, EventEmitter, NgZone } from '@angular/core';
import { MatAutocompleteTrigger } from '@angular/material/autocomplete';

@Directive({ selector: '[mat-tab-directive]' })
export class MatTabDirective implements AfterViewInit, OnDestroy {
    observable: any;
    constructor(@Optional() private autoTrigger: MatAutocompleteTrigger, private zone: NgZone) {
    }

    @Output()
    onMatTabSelectionChange = new EventEmitter<any>(true);

    ngAfterViewInit() {
        this.zone.runOutsideAngular(() => {
            this.observable = this.autoTrigger.panelClosingActions.subscribe(x => {
                if (this.autoTrigger.activeOption) {
                    this.autoTrigger.writeValue(this.autoTrigger.activeOption.value)
                    this.onMatTabSelectionChange.emit(this.autoTrigger.activeOption);
                }
            })
        });

    }
    ngOnDestroy() {
        this.observable.unsubscribe();
    }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!