[英]focus for ng-select not working Angular 5
i'm trying to use focus for ng-select in angular 5 but its not working我正在尝试在 angular 5 中为 ng-select 使用焦点,但它不起作用
i'm working with ng2-select我正在使用ng2-select
How to use focus for ng-select in Angular 5 ?如何在 Angular 5 中为 ng-select 使用焦点?
<label class="input">
<input (blur)="goTo()" type="text">
</label>
<label class="select" >
<div *ngIf="items.length > 0" >
<ng-select #select [items]="items">
</ng-select>
</div>
</label>
@ViewChild('select') myElement: ElementRef;
goTo(){
this.myElement.element.nativeElement.focus();
}
Change this改变这个
goTo(){
this.myElement.element.nativeElement.focus();
}
to this,对此,
import { ChangeDetectorRef } from '@angular/core';
constructor (private cRef: ChangeDetectorRef) {}
// import 'SelectComponent' from your library for 'ng2-select'
goTo(){
let el = null;
if (this.items.length > 0) {
this.cRef.detectChanges();
el = (this.myElement.nativeElement as SelectComponent).element.nativeElement.querySelector('div.ui-select-container > input');
if (el) { el.focus(); }
}
}
You may have to check if the element is defined or not (or if you need an extra 'nativeElement' in there, but I'm basically reusing the logic in here .您可能需要检查元素是否已定义(或者您是否需要在其中添加一个额外的“nativeElement”,但我基本上是在此处重用逻辑。
Just a cautionary note, this may not be a stable fix though if the library updates to rename these classes or modify the template.只是一个警告,如果库更新以重命名这些类或修改模板,这可能不是一个稳定的修复。
Hope it helps.希望能帮助到你。
A quick reply, in this solution,each select elements are recorded for future usage (and in this case , blur).快速回复,在此解决方案中,每个选择元素都被记录以备将来使用(在本例中为模糊)。 This needs being adapted to your situation.
这需要根据您的情况进行调整。
import { Component, OnInit } from '@angular/core'; import { ViewChildren, QueryList } from '@angular/core'; @Component({ selector: 'app-editor', templateUrl: './editor.component.html', styleUrls: ['./editor.component.css'] }) export class EditorComponent implements AfterViewInit { // Viewchildren // https://netbasal.com/understanding-viewchildren-contentchildren-and-querylist-in-angular-896b0c689f6e myindex:number; @ViewChildren("select") inputs: QueryList<any> constructor() { this.myindex=0 } // You will have to setup some input parameters in this function in order to switch to the right "select", (you have control) private goTo() { // focus next input field if (this.myindex +1 < this.inputs.toArray().length) { this.inputs.toArray()[this.myindex +1].nativeElement.focus(); this.myindex = this.myindex + 1; } else { this.inputs.toArray()[0].nativeElement.focus(); this.myindex = 0; } } private processChildren(): void { console.log('Processing children. Their count:', this.inputs.toArray().length) } ngAfterViewInit() { console.log("AfterViewInit"); console.log(this.inputs); this.inputs.forEach(input => console.log(input)); // susbcribe to inputs changes, each time an element is added (very optional feature ...) this.inputs.changes.subscribe(_ => this.processChildren() // subsequent calls to processChildren ); } }
this works fine for me,这对我来说很好用
@ViewChild('ngSelect') ngSelect: NgSelectComponent;
ngAfterViewInit() {
if (this.autofocus) {
setTimeout(() => {
this.ngSelect.focus();
});
}
}
refer https://github.com/ng-select/ng-select/issues/762参考https://github.com/ng-select/ng-select/issues/762
A Simple way, you can do this also.一个简单的方法,你也可以这样做。
<label class="input">
<input (blur)="goTo(select)" type="text">
</label>
<label class="select">
<div *ngIf="items.length > 0">
<ng-select #select [items]="items">
</ng-select>
</div>
</label>
And in Typescript File.并在打字稿文件中。
goTo(select){
select.focus();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.