【问题标题】:Ionic ElementRef access the native elementIonic ElementRef 访问原生元素
【发布时间】:2018-08-30 15:05:30
【问题描述】:

我写了一个指令来自动改变用户输入时 ion-textarea 的高度。但是,改变 ion-textarea 本身的高度是行不通的。我的指令中更改高度的代码:

 adjust(): void {
      console.log(this.element.nativeElement);
        let nativeElement = this.element.nativeElement;
        nativeElement.style.overflow = 'hidden';
        nativeElement.style.height = 'auto';
        nativeElement.style.height = nativeElement.scrollHeight + "px";
    }

我怀疑高度不会改变,因为 this.element.nativeElement 是 ion-textarea(而不是 textarea)。如何访问本机文本区域以更改其高度?

这里有一个 STACKBLITZ 来说明问题。该指令为auto-resize指令,带有ion-textarea的页面为首页。

【问题讨论】:

    标签: angular ionic-framework ionic3


    【解决方案1】:

    因此可以在指令的主机侦听器中访问本机文本区域:

    @HostListener('input', ['$event.target'])
    onInput(textArea: HTMLTextAreaElement): void {
        this.adjust(textArea);
    }
    

    在此之后,可以在 adjust() 函数中操作文本区域。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-08
      • 2020-05-12
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      相关资源
      最近更新 更多