Angular 插入角为4和x2B的插入符号;

Angular 插入角为4和x2B的插入符号;,angular,Angular,在特定光标位置将插入符号插入到文本区域时,我完全被角度投影卡住了。我尝试将标签/文本动态插入到选定光标位置的文本区域 你能帮帮我吗 谢谢, Sreepriya J如果要将光标聚焦在特定位置,则需要使用selectionStart native元素 var startPos=this.r.nativeElement.selectionStart; this.r.nativeElement.focus(); 然后需要将前一个值与当前值合并 this.r.nativeElement.value=th

在特定光标位置将插入符号插入到文本区域时,我完全被角度投影卡住了。我尝试将标签/文本动态插入到选定光标位置的文本区域

你能帮帮我吗

谢谢,
Sreepriya J

如果要将光标聚焦在特定位置,则需要使用selectionStart native元素

var startPos=this.r.nativeElement.selectionStart;
this.r.nativeElement.focus();
然后需要将前一个值与当前值合并

this.r.nativeElement.value=this.r.nativeElement.value.substr(0,this.r.nativeElement.selectionStart)+e+this.r.nativeElement.value.substr(this.r.nativeElement.selectionStart,this.r.nativeElement.value.length);

检查此处的示例:

一种更简单的方法,尝试在光标处插入文本 首先,安装并导入

npm install --save insert-text-at-cursor

import insertTextAtCursor from 'insert-text-at-cursor';
用法


你能在stackblitz中添加你的代码吗?我想用Angular4+a完成这项工作。也尝试过了。它没有保留上次的选择。你找到解决方案了吗?
// Find an element you want on the page
const el = document.getElementById('my-textarea');
insertTextAtCursor(el, 'foobar');