简体   繁体   English

角度:保留光标在被替换元素中的位置

[英]Angular: Preserving cursor position within replaced element

From a high level: I have inherited some complex form manipulation code that has a major usability bug--editing a text field moves the cursor to the end of the entered text after each change. 从高层次上讲:我继承了一些复杂的表单操作代码,这些代码有一个主要的可用性错误-编辑文本字段将每次更改后将光标移动到输入文本的末尾。

I looked at this question which seems close, but not quite answering my question because the elements in question are using the include-replace pattern. 我看着这个问题似乎很近,但是并没有完全回答我的问题,因为有问题的元素正在使用include-replace模式。

I'm having a hard time figuring out how to combine these approaches. 我很难弄清楚如何组合这些方法。 I don't want to change the entered text, just make sure the cursor doesn't hop around. 我不想更改输入的文本,只需确保光标不会跳来跳去。

As I understand it, the link function gets called when the partial is recompiled, which is happening whenever a change to the underlying model occurs, which happens every time the user edits the field at all. 据我了解,链接函数是在重新编译部分函数时调用的,只要对基础模型进行了更改,即在每次用户完全编辑字段时都会发生。 I can capture the cursor location by adding an event handler to the link function of my include-replace, but this doesn't have access to the element that is going to be swapped in. 我可以通过将事件处理程序添加到我的include-replace的链接函数中来捕获光标位置,但是它无权访问将要交换的元素。

myModule.directive('includeReplace', function () {
return {
    require: 'ngInclude',
    restrict: 'A', /* optional */
    link: function (scope, el, attrs) {
        el.replaceWith(el.children());

        el.on('change', function(event){
         var cursorPosition = event.target.selectionEnd;
         console.log(cursorPosition); // where I expect it
         el.selectionEnd; = cursorPosition; // but obviously this don't work
        });
        }

    }; 
});

I definitely don't have a super-strong grasp of the whole angular compile/link lifecycle, though I have read all the docs more than once. 尽管我不止一次阅读所有文档,但我对整个角度编译/链接生命周期绝对没有很强的了解。 A comprehensive flow-chart would be nice... 全面的流程图会很好...

Instead doing: 而是这样做:

el.on('change', function(event){
         var cursorPosition = event.target.selectionEnd;
         console.log(cursorPosition); // where I expect it
         el.selectionEnd; = cursorPosition; // but obviously this don't work
        });
        }

What about doing: 怎么做:

scope.$watch(function () {
  return el.val();
}, function (value) {
  $timeout(function(){
    var cursorPosition = event.target.selectionEnd;
    console.log(cursorPosition); // where I expect it
    el.selectionEnd = cursorPosition;
  });
});

Don't forget to include $timeout in your directive. 不要忘记在指令中包含$timeout

Hope this helps! 希望这可以帮助!

Well, for my purposes, it turns out I simply needed to add the ng-model-options="{ updateOn: 'blur' }" to the html. 好吧,出于我的目的,事实证明我只需要在html中添加ng-model-options="{ updateOn: 'blur' }" This prevents the model from updating and triggering the replace until the user is finished editing. 这样可以防止模型更新和触发替换,直到用户完成编辑为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM