简体   繁体   English

使用Angular Material 2在文本区域中查找选定的文本

[英]Find selected text in textarea with Angular Material 2

How do you find the selected text in a textarea in an Angular Material 2 input component? 如何在Angular Material 2输入组件的textarea中找到选定的文本? I need to create a directive for it. 我需要为此创建一个指令。

Handle the select event of your input: 处理输入的select事件:

<md-input-container>
  <input mdInput placeholder="Favorite food" value="Sushi" (select)="selectionchange($event)">
</md-input-container>

Then, use the approach from the jQuery: get input selection range thread to get the selected text: 然后,使用jQuery中的方法:获取输入选择范围线程以获取所选文本:

selectionchange(ev:any) {
  const start = ev.target.selectionStart;
  const end = ev.target.selectionEnd;
  console.log(ev.target.value.substr(start, end - start))
}

See the plunk that illustrates this. 看到说明这一点的小家伙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM