在 AngularJs 中获取文本区域光标位置

Get Textarea Cursor Position in AngularJs

这个问题归结为需要获取指令中 <textarea> 元素的光标位置。简而言之,我需要在这方面工作。

var position = element.selectionStart;

至少有 5 个关于该主题的答案使用各种方法,例如this one for angular, this one for some jQuery extension, enter link description here.

我已经全部阅读并决定尝试获取选择起始对象。这是我的 PLNKR。我不知道我做错了什么。注意 - 背景变化的东西是看我是否选择了正确的元素,我是。此处编辑失败的代码片段:

      link: function(scope, elem, attrs) {
          var textArea = elem.find("#itemTextArea");
          textArea.bind('click', function() {
              //This gets to undefined, even though selectionStart is a property of textarea*
              scope.testValue = elem.selectionStart;
          });
      },

*https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Property/selectionStart

P.S。单击文本区域可立即了解指令中发生的情况。

您要查找的语法是:

scope.testValue = textArea.prop("selectionStart");

这似乎也有效

scope.testValue = textArea[0].selectionStart;