gpt4 book ai didi

angular - 自动对焦只工作一次

转载 作者:太空狗 更新时间:2023-10-29 17:07:16 24 4
gpt4 key购买 nike

单击输入时,自动对焦仅在第一个实例上起作用 - 因此“列表格式化程序”(autocompleListFormatter) 仅启动一次。

**Demo**

是否有一种简单的解决方案可以使“自动对焦”多次对焦?

dropdown.component.html

<form [formGroup]="myForm" class="">
<div class="form-style">
<input
autofocus
[list-formatter]="autocompleListFormatter"
type="text"
class="form-control"
ngui-auto-complete
formControlName="costCenter"
[source]="dropdownData"
value-property-name="id"
display-property-name="name"
[(ngModel)]="value"
/>
</div>
</form>

dropdown.component.ts

export class DropdownComponent implements OnInit, AgEditorComponent {
@Input() name: String;

public dropdownData = ColumnData[0].cellEditorParams.values;
public myForm: FormGroup;
public selected;
value: any;
oldValue: any;
params: any;
public container;
constructor(private builder: FormBuilder, private _sanitizer: DomSanitizer) {}

// ****DROPDOWN****** //
autocompleListFormatter = (data: any) => {
let html = `<span>${data.name}</span>`;
return this._sanitizer.bypassSecurityTrustHtml(html);
};

refresh(params: ICellEditorParams) {
this.params.api.refreshCells();
return true;
}

getValue(): any {
if (this.value === '') {
this.value = this.oldValue;
}
return this.value;
}

agInit(params: ICellEditorParams) {
this.value = params.value;
this.oldValue = this.value;
this.value = '';
return this.value;
}

ngOnInit() {
this.myForm = this.builder.group({
costCenter: ''
});
}
}

STACKBLITZ

更新:我读到实现 auto-focus directive 很有用.我已将该指令添加到我的代码中,但无法使其正常运行

最佳答案

这可以在没有指令的情况下完成,只需获取输入元素引用并运行 focus 方法

模板

<form [formGroup]="myForm" class="">
<div class="form-style">
<input
defFocus
[list-formatter]="autocompleListFormatter"
type="text"
class="form-control"
ngui-auto-complete
formControlName="costCenter"
[source]="dropdownData"
value-property-name="id"
display-property-name="name"
[(ngModel)]="value"
#agInput
/>
</div>
</form>

组件

export class DropdownComponent implements OnInit, AgEditorComponent {

@Input() name: String;
@ViewChild('agInput', { static: true}) public elm: ElementRef;

....

setFocus() {
this.el.nativeElement.focus();
}

ngAfterViewInit() {
setTimeout(() => {
this.setFocus();
}, 500);
}
}

demo 🚀

检查我的答案👉 here我在其中创建自定义指令来修复焦点问题。

关于angular - 自动对焦只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57989346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com