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

140
Views
mat-select not focused again after clicking a button

I have this field in my reactive form which is present inside a matDialog

<mat-form-field
                                class="w-full"
                                *ngIf="formType == 'da_banker'"
                            >
                                <mat-select
                                    value=""
                                    (selectionChange)="
                                        onTxnStatusChange($event.value)
                                    "
                                    formControlName="txn_status_banker"
                                    name="txn_status_banker"
                                    #setFocus
                                >
                                    <mat-option value=""
                                        >Select Transaction Status</mat-option
                                    >
                                    <mat-option
                                        value="{{ row.value }}"
                                        *ngFor="let row of txnUpdateOptions"
                                        [ngClass]="
                                            row.name == 'Pending'
                                                ? 'hidden'
                                                : ''
                                        "
                                        >{{ row.name }}</mat-option
                                    >
                                </mat-select>
                                <mat-error *ngIf="showTxnStatusError"
                                    >Required</mat-error
                                >
                            </mat-form-field>

I am focusing this field when the view initializes using

@ViewChild('setFocus') setFocus:ElementRef;

ngAfterViewInit(): void {
        this.setFocus['_elementRef'].nativeElement.focus();
    }

it is working fine but when i submit the form i am not closing it as i have to fill it again for next data. For this, i want to focus the same field again and for this i am using this code after the submit button clicks

updateTxnStatus() {

        

        if(this.form.invalid == true){
            return;
        }

            this.setFocus['_elementRef'].nativeElement.focus();

            console.log(this.setFocus['_focused']);
            console.log(this.setFocus);

            console.log(document.activeElement)

            this.form.controls['txn_status_banker'].setValue('');

        
    }

In console the active focused element is exactly what i want but in view it is not being focused.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your focus() may fail if this.form.invalid is true or changes to the form clear the focus. Move .focus() the end of the block, make sure focus() is fired.

about 4 years ago · Juan Pablo Isaza Report
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!