Caret insert in Angular4+

2019-09-02 20:39发布

问题:

I am totally stuck with angular project in doing caret insert to text area at a particular cursor position. I try to insert labels/text dynamically to text area in selected cursor positions.

Can you please help me?

Thanks, Sreepriya J

回答1:

If you want to focus the cursor at specific position you need to use selectionStart native Element

var startPos=this.r.nativeElement.selectionStart;
this.r.nativeElement.focus();

Then you need to concat the previous value with current value

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);

Check the Example Here: https://stackblitz.com/edit/angular-hn8unq



标签: angular