如何在Angular 2中获得一个可疑的'div'的插入位置?

时间:2016-12-02 21:14:08

标签: html angular typescript

我有以下代码:

<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
}

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

1 个答案:

答案 0 :(得分:1)

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.' />

希望这会有所帮助!!