0

I have more than 25 text boxes and I need to make the focus on each text boxes based on the user input.

But I don't want to make 25 different reference variables in my HTML also it will affect my angular code being more length.

I am using template

<input type="text" [(ngModel)]="textVal" #textValue>

and my class I used

@ViewChild('textValue') textValue;

this.textValue.nativeElement.focus();

So my question is can I need to create 25 reference variables and 25 view child variables

can anybody give some suggestions??

shajan
  • 610
  • 6
  • 11

1 Answers1

0

A possible solution is to use @ViewChildren in your component:

@ViewChildren('input[type=text]')
allInputs;

That way you can set the focus on any of those inputs based on your business logic. No need to use reference variables anymore.

alcfeoh
  • 2,147
  • 1
  • 18
  • 33