输入光标位置以 ng-change() 跳转到结尾 [英] Input cursor position jumps to end with ng-change()
问题描述
我有一个实例,我正在通过 ngChange
替换 ngModel
的值.每次更改后,光标都会跳转到 input
字段的末尾(假设因为我将结果分配给相同的 $scope
变量.)
我想知道如何防止这种行为?
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);
};
问题示例:如果用户输入 1000.没关系.但是,如果他们想返回并将数字更改为 156000,只需将 5 和 6 相加,实际上将 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 goes 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.
这个例子可能对你有帮助:链接
This example might help you: Link
这篇关于输入光标位置以 ng-change() 跳转到结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!