如何在飞镖中自动增长文本区域元素?

How to autogrow a textarea element in dart?

我想要一个 textarea 元素,它可以根据 textarea 元素的内容动态调整其高度。

我如何在 Dart 中实现它?

仅使用纯 dart:html:

var textArea = querySelector('textarea');
textArea.onInput.listen((_) {
  // shrink the textarea when needed
  textArea.style.height = 'auto';

  // set the height to scrollHeight plus some correction
  var correction = textArea.offsetHeight - textArea.clientHeight;
  textArea.style.height = '${textArea.scrollHeight - correction}px';
});

你也可以做一个 angular2 指令:

@Directive(
    selector: 'textarea[autogrow]',
    host: const {
      '(input)': 'onInput($event.target)'
    }
)
class AutogrowDirective {

  onInput(TextAreaElement textArea) {
    // shrink the textarea when needed
    textArea.style.height = 'auto';

    // set the height to scrollHeight plus some correction
    var correction = textArea.offsetHeight - textArea.clientHeight;
    textArea.style.height = '${textArea.scrollHeight - correction}px';
  }
}