如何在dart中自动生成textarea元素?

时间:2015-10-21 21:37:19

标签: dart

我希望有一个textarea元素,可以动态调整其高度到textarea元素的内容。

我怎么能在Dart中实现这个?

1 个答案:

答案 0 :(得分:6)

只需使用纯粹的飞镖: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';
  }
}