Angular 4 set the focus in the dynamic generated textbox using viewChildran

大兔子大兔子 提交于 2019-12-24 01:21:21

问题


I have the dynamically generated textbox as below.

<tr *ngFor="let item of data; let in=index">
    <td>
                        <input #unitNumber type="text" name="workPerformed-workcode-{{in}}" [(ngModel)] = "item.unitnumber" >
                      </td>
<td> <!-- Search option is given to chose the unit number----></td>
</tr>

Here, the search option is given to choose the unit number, if it has been chosen, then the corresponding textbox will be focused on using viewChildran.

My try is

@ViewChildren('unitNumber') enteredUnitNumbers;

// for searching, I have used the material dialog box
const dialogRef = this.dialog.open(SearchEquipmentComponent, dialogConfig);

    dialogRef.afterClosed().subscribe(
      <!-- HERE I NEED TO DO THE FOCUS ON PARTICULAR TEXTBOX ---->
     // console.log(this.enteredUnitNumbers.toArray().map(x => x))
});

Above console.log shows undefined. My need is that once dialog box is closed the corresponding unit number textbox should be focused.

Kindly give solutions


回答1:


The following should do it:

enteredUnitNumbers.toArray()[0].nativeElement.focus();

Replace 0 with the index of the desired input.



来源:https://stackoverflow.com/questions/52898230/angular-4-set-the-focus-in-the-dynamic-generated-textbox-using-viewchildran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!