如何在 ngFor 中使用引用 ID ( # ) 并在下一个 Ng select 中获得焦点?
how to use reference id ( # ) in ngFor and get focus next Ng select?
我有循环 ngFor,我需要用索引声明引用 ID“#”。例如
<button (click)="addRow()"></button>
<tr *ngFor="let data of datas; let i= index">
<td><ng-select #data{{i}} ></ng-select></td>
</tr>
addRow(){
// after selected data next row to focus.
}
我想关注下一行 ng-select.
尝试使用下面的代码
<tr *ngFor="let dataObject of data; let i = index; trackBy:i;">
<td><ng-select #data{{i}} ></ng-select></td>
</tr>
对焦用
import { Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
...
@ViewChild('data1') inputEl:ElementRef;
addRow() {
setTimeout(() => this.inputEl.nativeElement.focus());
}
或
import Renderer2 from @angular/core into your component. Then:
const element = this.renderer.selectRootElement('#data1');
setTimeout(() => element.focus(), 0);
引用https://coderanch.com/t/675897/languages/programmatically-manage-focus-Angular-app
更新 Viewchildren 一个输入或一个ng-select
通过输入,您可以使用 ViewChildren
<div *ngFor="let item of items">
<input #data/>
</div>
@ViewChildren('data') data: QueryList<ElementRef>;
ngAfterViewInit()
{
this.data.changes.subscribe(()=>{
this.data.last.nativeElement.focus();
})
}
如果我们有 ng-select 你需要
<div *ngFor="let item of items">
<ng-select #data .....>
</ng-select>
</div>
<!--see that the "QueryList" is a NgSelectComponent-->
@ViewChildren('data') data: QueryList<NgSelectComponent>;
ngAfterViewInit()
{
this.data.changes.subscribe(()=>{
<!--we use filterInput.nativeElement-->
this.data.last.filterInput.nativeElement.focus();
})
}
A full stackblitz(在 stackblitz 中我添加了一个 "takeWhile" 来取消订阅 ngOnDestroy 元素中的更改)
我有循环 ngFor,我需要用索引声明引用 ID“#”。例如
<button (click)="addRow()"></button>
<tr *ngFor="let data of datas; let i= index">
<td><ng-select #data{{i}} ></ng-select></td>
</tr>
addRow(){
// after selected data next row to focus.
}
我想关注下一行 ng-select.
尝试使用下面的代码
<tr *ngFor="let dataObject of data; let i = index; trackBy:i;">
<td><ng-select #data{{i}} ></ng-select></td>
</tr>
对焦用
import { Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
...
@ViewChild('data1') inputEl:ElementRef;
addRow() {
setTimeout(() => this.inputEl.nativeElement.focus());
}
或
import Renderer2 from @angular/core into your component. Then:
const element = this.renderer.selectRootElement('#data1');
setTimeout(() => element.focus(), 0);
引用https://coderanch.com/t/675897/languages/programmatically-manage-focus-Angular-app
更新 Viewchildren 一个输入或一个ng-select
通过输入,您可以使用 ViewChildren
<div *ngFor="let item of items">
<input #data/>
</div>
@ViewChildren('data') data: QueryList<ElementRef>;
ngAfterViewInit()
{
this.data.changes.subscribe(()=>{
this.data.last.nativeElement.focus();
})
}
如果我们有 ng-select 你需要
<div *ngFor="let item of items">
<ng-select #data .....>
</ng-select>
</div>
<!--see that the "QueryList" is a NgSelectComponent-->
@ViewChildren('data') data: QueryList<NgSelectComponent>;
ngAfterViewInit()
{
this.data.changes.subscribe(()=>{
<!--we use filterInput.nativeElement-->
this.data.last.filterInput.nativeElement.focus();
})
}
A full stackblitz(在 stackblitz 中我添加了一个 "takeWhile" 来取消订阅 ngOnDestroy 元素中的更改)