Angular 4 使用 viewChildran 在动态生成的文本框中设置焦点
Angular 4 set the focus in the dynamic generated textbox using viewChildran
我有如下动态生成的文本框。
<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>
这里给出了搜索选项,可以选择单元号,如果已经选择了,那么对应的textbox会重点使用viewChildran。
我的尝试是
@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))
});
以上 console.log 显示未定义。我的需要是,一旦对话框关闭,相应的单元号文本框就应该成为焦点。
请给出解决方案
下面应该这样做:
enteredUnitNumbers.toArray()[0].nativeElement.focus();
将0
替换为所需输入的索引。
我有如下动态生成的文本框。
<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>
这里给出了搜索选项,可以选择单元号,如果已经选择了,那么对应的textbox会重点使用viewChildran。
我的尝试是
@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))
});
以上 console.log 显示未定义。我的需要是,一旦对话框关闭,相应的单元号文本框就应该成为焦点。
请给出解决方案
下面应该这样做:
enteredUnitNumbers.toArray()[0].nativeElement.focus();
将0
替换为所需输入的索引。