Local variable inside an ngFor

Asked

Viewed 198 times

0

I have an input text inside an ngFor and would like to define a dynamic local variable that has a unique id value for each input, so that I can perform the . Focus() of this input, but I don’t know how to accomplish it, I’ve tried it in several ways and I haven’t succeeded.

I would like to set the idEmail of each email as my local variable In order to access it with my @Viewchild

<tr *ngFor="let email of Emails">
    <td *ngIf="!isUpdating(email.idEmail)">
        {{ email.enderecoEmail }}
    </td>
    <td *ngIf="isUpdating(email.idEmail)">
        <input [(ngModel)]="txtEmail" #email.idEmail type="text" class="form-control">
    </td>
</tr>

Please help me I’ve been stuck in this task for 2 days and I can’t find the solution

1 answer

0


Hello, welcome to Sopt.

Good to solve your problem we can use the attr in the input to set its id.

Ex.

<input attr.id="input_email_{{email.idEmail}}">

Then just capture the same and execute what you wish.

Ex.

let thatemail = document.getElementById('input_email_2');

thatemail.focus();

Complete example in: https://stackblitz.com/edit/angular-lxbhul

Browser other questions tagged

You are not signed in. Login or sign up in order to post.