Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set matButton active (focused) Angular?

When first and following time user enters the page, placed button is not focused and highlighted by gray color. I tried to set autofocus:

<button mat-raised-button color="primary" autofocus>{{'recovery' | translate}}</button>

I consider that it is imperfection of Material buttons.

How to set focused material button in last Angular?

like image 914
OPV Avatar asked Jul 11 '26 03:07

OPV


1 Answers

You can just set focus to the button at component's life-cycle hook ngAfterViewInit for example:

Template

<button mat-raised-button color="primary" #btnRef="matButton">{{'recovery' | translate}}</button>

Ts file

...
@ViewChild('btnRef') buttonRef: MatButton;
...
ngAfterViewInit() {
   this.buttonRef.focus();
}
...

Hope that helps.

like image 149
Amir Arbabian Avatar answered Jul 15 '26 11:07

Amir Arbabian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!