Angular Material Autocomplete force selection

Discussão em 'Angular' iniciado por Alessandro Celeghin, Novembro 26, 2024.

  1. In my angular 5 application, I have some matAutocomplete, but I want to force the selection of one of the suggestions, so I am following this approach: stackblitz but for some reason in one case I have an issue:


    Cannot read property 'panelClosingActions' of undefined at CustomerDetailComponent.countryClosingActions (customer-detail.component.ts:199) at CustomerDetailComponent.ngAfterViewInit
    ​

    I have multiple matAutocomplete but only this one have problems. (info about this method is here github

    html

    <mat-form-field>
    <input matInput #nation placeholder="{{'customer.detail.labels.country'
    | translate }}" required [matAutocomplete]="tdAuto" name="country"
    #count="ngModel" [(ngModel)]="selected.country"
    (ngModelChange)="searchCountry($event)">
    <mat-autocomplete #tdAuto="matAutocomplete" [displayWith]="displayFn">
    <mat-option (onSelectionChange)="setCountry(country)" *ngFor="let country of countries" [value]="country">
    <div class="row">
    <img src="assets/img/flags24/{{country.alpha2Code | lowercase}}.png" />
    <span>{{country.name}} ({{country.alpha2Code}})</span>
    </div>
    </mat-option>
    </mat-autocomplete>
    </mat-form-field>


    component

    @ViewChild('nation', { read: MatAutocompleteTrigger }) trigger: MatAutocompleteTrigger;
    subscription: Subscription;


    ngAfterViewInit() {
    this.countryClosingActions();
    }

    private countryClosingActions(): void {
    if (this.subscription && !this.subscription.closed) {
    this.subscription.unsubscribe();
    }

    this.subscription = this.trigger.panelClosingActions
    .subscribe(e => {
    console.log('closing')
    if (!e || !e.source) {
    this.selected.country = null;
    this.selfCountry = null;
    }
    },
    err => this.countryClosingActions(),
    () => this.countryClosingActions());
    }

    Continue reading...

Compartilhe esta Página