在angular2中自动调整textarea

vip*_*pin 10 textarea autosize angular

我正在开发angular2 applciation.我需要自动调整textarea.我试图从https://github.com/stevepapa/angular2-autosize重用angular2-autosize

跟着自述文件,但我得到以下错误

未捕获错误:模块构建失败:错误:ENOENT:没有此类文件或目录,打开'C:\ Users\Vipin\SampleApp \node_modules\angular2-autosize\angular2-autosize.js'.

请建议如何克服这个问题.

chr*_*ian 16

更新(15.04.2018)管理打包它,现在它可用

npm install ngx-autosize
Run Code Online (Sandbox Code Playgroud)

https://github.com/chrum/ngx-autosize

老答案:

我今天遇到了同样的问题,并修好了!请检查我的分支:https: //github.com/chrum/angular2-autosize

在PR合并之前尝试:

npm install https://github.com/chrum/angular2-autosize.git --save
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中,因为它略有不同,你只需导入模块而不是指令......

因此 ,而不是:

import {Autosize} from 'angular2-autosize';

@NgModule({
  ...
  declarations: [
    Autosize
  ]
  ...
})
Run Code Online (Sandbox Code Playgroud)

你应该:

import {AutosizeModule} from 'angular2-autosize';

@NgModule({
  ...
  imports: [
    AutosizeModule
  ]
  ...
})
Run Code Online (Sandbox Code Playgroud)

  • 好的,我很快检查了一下,在我所有的项目中,我都必须将打字稿锁定在2.4.2才能正常工作...我想是时候适当地打包并在npm上发布了,所以没有类似的问题 (2认同)

tan*_*dar 12

如果不使用包,你可以这样做.这很简单

在控制器如下

autogrow(){
  let  textArea = document.getElementById("textarea")       
  textArea.style.overflow = 'hidden';
  textArea.style.height = '0px';
  textArea.style.height = textArea.scrollHeight + 'px';
}
Run Code Online (Sandbox Code Playgroud)

和html一样,如下所示

<textarea id="textarea" (keyup)="autogrow()" ></textarea>
Run Code Online (Sandbox Code Playgroud)

  • 虽然这有效,但它打破了Angular的哲学(即不直接操纵DOM).而不是使用document.getElementById,使用@ViewChild获取textarea的引用(因为它毕竟是组件的子代) (9认同)

San*_*isy 6

该解决方案在 IE 和其他浏览器上都对我有用

// Usage example: <textarea autoresize></textarea>

import { ElementRef, HostListener, Directive} from '@angular/core';

@Directive({
    selector: 'textarea[autosize]'
})

export class Autosize {
 @HostListener('input',['$event.target'])
  onInput(textArea: HTMLTextAreaElement): void {
    this.adjust();
  }
  constructor(public element: ElementRef){
  }
  ngAfterContentChecked(): void{
    this.adjust();
  }
  adjust(): void{
    this.element.nativeElement.style.overflow = 'hidden';
    this.element.nativeElement.style.height = 'auto';
    this.element.nativeElement.style.height = this.element.nativeElement.scrollHeight + "px";
  }
}
Run Code Online (Sandbox Code Playgroud)

将以下代码添加到 APp.Module.ts

@NgModule({
  declarations: [
    Autosize
  ]
})
Run Code Online (Sandbox Code Playgroud)

在 HTML 上使用标签

 <textarea autosize></textarea>
Run Code Online (Sandbox Code Playgroud)


dil*_*111 5

tanveer答案的稍作修改的答案是使用@ViewChild

@ViewChild('textArea', { read: ElementRef }) textArea: ElementRef;

public autoGrow() {
 const textArea = this.textArea.nativeElement;
 textArea.style.overflow = 'hidden';
 textArea.style.height = '0px';
 textArea.style.height = textArea.scrollHeight + 'px';
}
Run Code Online (Sandbox Code Playgroud)

在HTML中

<textarea (keyup)="autoGrow()" #textArea></textare>
Run Code Online (Sandbox Code Playgroud)


Cht*_*lek 5

为什么需要此插件,它很简单:

<textarea (keyup)="autoGrowTextZone($event)"></textarea>
Run Code Online (Sandbox Code Playgroud)

autoGrowTextZone(e) {
  e.target.style.height = "0px";
  e.target.style.height = (e.target.scrollHeight + 25)+"px";
}
Run Code Online (Sandbox Code Playgroud)


Jen*_*ens 5

所请求的行为已经在angular material此处实现,如Angular Material Input Autosize所示angular material无论如何,这特别有用。

只需cdkTextareaAutosize在示例中使用:

<textarea cdkTextareaAutosize></textarea>
Run Code Online (Sandbox Code Playgroud)

  • @SnowBases,不是真的。并非每个人都希望材质输入引入所有输入滞后。 (2认同)

ahm*_*gar 5

简单地

您可以如下使用:

<textarea [rows]="text.split('\n').length || 2">{{text}}</textarea>
Run Code Online (Sandbox Code Playgroud)

或者

在 ts 中创建一个函数:

  getHeight(content) {
    const v1 = Math.floor(content.length / 50);
    const v2 = content.split('\n').length;
    return Math.max(v1,v2)
  }
Run Code Online (Sandbox Code Playgroud)

HTML:

<textarea [rows]="getHeight(text) || 2">{{text}}</textarea>
Run Code Online (Sandbox Code Playgroud)