Angular输入自动大小与typeahead不起作用
创始人
2024-10-29 15:00:57
0

使用ngAfterViewInit和setTimeout函数来延迟自动大小初始化

在Angular中,当使用Typeahead控件时,自动跟踪控件的大小可能会出现问题。这是因为Typeahead创建了一个动态内容,可能在组件的生命周期钩子函数ngOnInit或ngAfterViewInit结束后再加载。当Angular尝试为不稳定的元素计算自动大小时,它会返回错误的结果。

为了解决这个问题,我们可以使用ngAfterViewInit和setTimeout函数来延迟自动大小初始化,等到Typeahead控件完全加载后再计算。下面是示例代码:

import { Component, ElementRef, ViewChild } from '@angular/core';

@Component({ selector: 'app-typeahead-example', template: }) export class TypeaheadExampleComponent { @ViewChild('inputElement', { static: true }) inputElement: ElementRef;

selectedItem: string; dataSource = ['Apple', 'Banana', 'Cherry', 'Durian'];

ngAfterViewInit() { setTimeout(() => { this.inputElement.nativeElement.setAttribute('size', this.selectedItem.length + 2); }, 100); }

onChange(event) { if (event && event.length) { this.inputElement.nativeElement.setAttribute('size', event.length + 2); } else { this.inputElement.nativeElement.removeAttribute('size'); } } }

在上面的示例中,我们通过ViewChild装饰器获取了输入元素的引用,并在ngAfterViewInit钩子函数中使用setTimeout函数来延迟自动大小的初始化。在onChange事件中,我们根据选择的项目或输入的字符数来调整输入元素的大小。

相关内容

热门资讯

透视步骤!aapoker怎么设... 透视步骤!aapoker怎么设置抽水,wepoker作必弊辅助(透视)好像真的有挂(哔哩哔哩)1、全...
透视秘籍!wepoker俱乐部... 透视秘籍!wepoker俱乐部辅助,wepokerplus脚本(透视)本来存在有挂(哔哩哔哩)1、用...
透视指南书!wepoker俱乐... 透视指南书!wepoker俱乐部辅助器,聚星ai辅助工具下载(透视)切实是有挂(哔哩哔哩)1、下载好...
透视秘籍!aapoker透视脚... 透视秘籍!aapoker透视脚本,wpk辅助器是真的吗(透视)原来是有挂(哔哩哔哩)1、wpk辅助器...
透视练习!佛手在线是不是有挂,... 透视练习!佛手在线是不是有挂,hhpoker德州真的假的(透视)确实真的有挂(哔哩哔哩)1、每一步都...
透视手册!uupoker透视,... 透视手册!uupoker透视,werplan脚本(透视)真是真的有挂(哔哩哔哩)1、werplan脚...
透视烘培!德普之星辅助器怎么用... 透视烘培!德普之星辅助器怎么用,微扑克微乐辅助(透视)其实存在有挂(哔哩哔哩)1、德普之星辅助器怎么...
透视窍门!hhpoker哪个俱... 透视窍门!hhpoker哪个俱乐部靠谱,hhpoker的辅助是真的吗(透视)切实是真的挂(哔哩哔哩)...
透视手筋!购买wepoker模... 透视手筋!购买wepoker模拟器,wepoker黑侠辅助器正版下载(透视)原来是有挂(哔哩哔哩)所...
透视秘籍!德普之星私人局辅助器... 透视秘籍!德普之星私人局辅助器,wepoker怎么开辅助(透视)总是真的有挂(哔哩哔哩)1)德普之星...