【发布时间】:2020-07-08 01:54:09
【问题描述】:
我正在使用 *ngFor 动态创建输入。所以,我想在最后创建的输入上设置 focus。有人可以帮我吗?
这是我的模板代码:
<div class="col-6">
<mat-card class="dataItens">
<mat-card-header>
<mat-card-title>Itens de dados</mat-card-title>
</mat-card-header>
<mat-card-content>
<section *ngFor="let dataItem of elementaryProcess.dataItens; let i = index;">
<mat-form-field class="input-dataItens">
<input matInput [(ngModel)]="dataItem.name" name="dataItem{{ i }}" id="dataItem{{ i }}"
autocomplete="off"
[ngModelOptions]="{standalone: true}" class="input-dataItens"
(keyup.arrowDown)="dataItemOnKeyUp()"/>
</mat-form-field>
<button type="button" mat-icon-button (click)="removeDataItem(i)">
<mat-icon>delete</mat-icon>
</button>
</section>
</mat-card-content>
<mat-card-actions>
<form>
<!-- Add button -->
<button mat-stroked-button color="primary" type="button" (click)="addDataItem()">Adicionar
</button>
<!-- ngFor code -->
</form>
</mat-card-actions>
</mat-card>
</div>
这是我用来添加输入的函数:
addDataItem() {
this.elementaryProcess.dataItens.push(new DataItem());
}
【问题讨论】:
标签: angular