输入光标位置跳跃与NG-变化结束() [英] Input cursor position jumps to end with ng-change()

查看:203
本文介绍了输入光标位置跳跃与NG-变化结束()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里,我通过 ngChange 替换 ngModel 的值的实例。光标跳到输入字段的每个变化结束后(假设,因为我分配结果相同 $范围变量。)结果
我想知道我可以prevent这种行为?

I have an instance where I'm replacing the value of ngModel through ngChange. The cursor jumps to the end of the input field after each change (assuming because I'm assigning the result to the same $scope variable.)
I'd like to know how I can prevent this behavior?

  $scope.compute1 = 0;
  $scope.compute2 = 10;

  $scope.math = function() {
    $scope.compute1 = parseInt($scope.compute1);
    $scope.compute2 = parseInt($scope.compute2);

    $scope.compute1 = parseInt($scope.compute1);
  };

小提琴

fiddle

问题举例:如果一个用户在1000它的罚款。不过,如果他们想回去,只需添加5和6更改号码156000,6实际上被追加到结束:15006.

Example of problem: if a user types in 1000. It's fine. But then if they want to go back and change the number to 156000 by just adding the 5 and 6, the 6 would actually be appended to the end: 15006.

推荐答案

光标里去到最后,因为我们通过修改数据 parseInt函数

The cursor goest to the end because we modify data by using parseInt.

我建议你到光标处存放前,然后就大功告成了做事情的时候,设置了回去。

I suggest you to store the caret position before and then when you're done doing things, set it back.

这个例子可以帮助你:<大骨节病> Link

This example might help you: Link

这篇关于输入光标位置跳跃与NG-变化结束()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆