如何在Angular 2中获得内容可编辑的“ div”的插入符位置?

swa*_*990 5 html typescript angular

我有以下代码:

<div #test (click)="onClick(test)" contenteditable="true">
            This text can be edited by the user.
</div>

@ViewChild('test') el:ElementRef;

constructor(private elementRef: ElementRef) {}

onClick(event) {
  console.log(this.el.nativeElement); 
  let cursor = this.el.nativeElement.selectionStart; //gives undefined
}
Run Code Online (Sandbox Code Playgroud)

当用户单击文本时,如何获取“ div”元素的光标位置?

Mad*_*jan 0

selectionStart是 HTMLInputElement 的属性。

<div #test (click)="onClick(test)" contenteditable="true">
            This text can be edited by the user.
</div>

to 

<input  #test (click)="onClick(test)"  
  value='This text can be edited by the user.' />
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!!