Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually set focus on mat-form-field in Angular 6

I am new to angular ,In my application I have a mat-dialog in which I have two forms namely Login and SignUp.

Once I open the dialog first time the auto focus is set on username field.problem is when I navigate to SignUp form on button click that form's FIrst Name field not get auto focused ,the same way navigate from signup to login the username field is now not get auto focused.

I have tried to some stackoverflow solutions but nothing is resolved my issue.

popupScreen.component.html

<form class="login" *ngIf="isLoginHide">     <mat-form-field>         <input matInput placeholder="username">     </mat-form-field>     <mat-form-field>         <input matInput placeholder="password">     </mat-form-field>      <button mat-button color="primary">Login</button>     <button mat-button (click)="hideLogin($event)" color="accent">SignUp</button> </form>  <form class="SignUp" *ngIf="isSignUpHide">     <mat-form-field>         <input matInput placeholder="First Name">     </mat-form-field>     <mat-form-field>         <input matInput placeholder="Last Name">     </mat-form-field>     <mat-form-field>         <input matInput placeholder="username">     </mat-form-field>     <mat-form-field>         <input matInput placeholder="password">     </mat-form-field>      <button mat-button (click)="hideSignUp($event)" color="primary">Login</button>     <button mat-button color="accent">SignUp</button> </form> 

can anyone help me to solve this .

like image 978
Zhu Avatar asked Sep 03 '18 04:09

Zhu


People also ask

How can I increase my mat form-field size?

The key to adjusting the field size is actually just adjusting the font-size in the surrounding container. Once you do that, everything else will scale with it. e.g.

What is MatFormFieldControl?

MatFormFieldControl. An interface which allows a control to work inside of a MatFormField .

How do you remove padding from mat form-field?

NOTE: As you remove the space you cannot put <mat-hint>hint</mat-hint> or <mat-error>error</mat-error> properly. Error and hint get inside the form-field. Turn off encapsulation of your component inside which you change the padding. You can add these css in global stylesheet without turning off view encapsulation.


1 Answers

Kindly use cdkFocusInitial attribute in input field where you want to focus.

<mat-form-field>     <input matInput placeholder="username" #usernameInput cdkFocusInitial> </mat-form-field> 
like image 102
kumardippu Avatar answered Sep 17 '22 04:09

kumardippu