angularjs 1 Failed to read the 'selectionStart' property from 'HTMLInputElement':

在找angularjs input(type='number')在获取焦点的时候,文本框内容选中效果,参考了:Select text on input focus,我直接复制他的code之后,在ionic中报"Uncaught InvalidStateError: Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection.",

尝试了 

---

this.setSelectionRange(0, 9999)

---

this.selectionStart = 0;
this.selectionEnd = 999;

上面两个选中文本,我在webapp中测试的时候,需要每次双击之后才会有效果,我尝试用jQuery的select()方法来达到这个效果,再ionic编译的apk中测试可用之后,觉得这是我目前的解决方法。

directive方法代码

.directive('selectOnClick',  function ($window) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            element.on('click', function () {
                if (!$window.getSelection().toString()) {
                    // Required for mobile Safari
                    $(this).select();
                }
            });
        }
    };
  })

input使用这个directive(指令)像:

<input type="text" value="test" select-on-click />

再android app中,使用type="number"也可以工作。

原文地址:https://www.cnblogs.com/fsong/p/5563119.html