2

I am new to Angular and I've been able to muddle my way through most things. I've been researching this for some time and cannot determine how to set the focus to a new FormControl created in a FormArray. Below is the relevant html code and the addRoom function. I want to set the focus to the new control in the addRoom function.

<div><span>Workspaces/Rooms</span><mat-icon (click)="addRoom()">add_circle_outline</mat-icon></div>
    <div class="indent">
      <div formArrayName="workspaces">
        <div *ngFor="let item of adminForm.get('workspaces')['controls'];  let j=index">
            <input type="text" formControlName = "{{j}}" autofocus/><mat-icon (click)="deleteRoom(j)">delete_forever</mat-icon>
        </div>
      </div>
    </div>

addRoom():void{
    (<FormArray>this.adminForm.controls['workspaces']).insert(0, new FormControl(null));
    //need code to set focus on new control
  }
michael
  • 31
  • 1
  • https://stackoverflow.com/questions/41190075/how-do-i-programmatically-set-focus-to-dynamically-created-formcontrol-in-angula does this answer your question? – naoval Jun 15 '21 at 03:28
  • That looks exactly like what I need. Thank you. – michael Jun 17 '21 at 19:15

0 Answers0