角度虚拟键盘无法在焦点上打开

时间:2017-12-07 12:14:06

标签: angularjs



<link href="https://rawgit.com/the-darc/angular-virtual-keyboard/master/release/angular-virtual-keyboard.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
<script src="https://rawgit.com/the-darc/angular-virtual-keyboard/master/release/angular-virtual-keyboard.js"></script>

<body ng-app='myApp'>
  <div ng-controller="MyCtrl">
    <input type='text' ng-model="textValue" ng-virtual-keyboard/>
  </div>
</body>
&#13;
input.focus()
&#13;
&#13;
&#13;

如果我点击输入,则虚拟键盘正在打开。但是如何在不点击的情况下打开键盘。我尝试使用无效的{{1}}。

2 个答案:

答案 0 :(得分:1)

找到一个带有指令自动对焦的工作小提琴Here

    var myApp = angular.module('myApp',['angular-virtual-keyboard']);

myApp.controller('MyCtrl',function($scope) {
});
  


myApp.directive('autoFocus', function($timeout) {
    return {
        restrict: 'AC',
        link: function(_scope, _element) {
            $timeout(function(){
                _element[0].focus();
            }, 0);
        }
    };
});
<link href="https://rawgit.com/the-darc/angular-virtual-keyboard/master/release/angular-virtual-keyboard.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
    <script src="https://rawgit.com/the-darc/angular-virtual-keyboard/master/release/angular-virtual-keyboard.js"></script>

    <body ng-app='myApp'>
      <div ng-controller="MyCtrl">
        <input type='text' ng-model="textValue" auto-focus ng-virtual-keyboard/>
      </div>
    </body>

答案 1 :(得分:0)

您正在angularJS框架之外进行更改,因此您需要通过$ scope应用它。$ apply();

var myApp = angular.module('myApp',['angular-virtual-keyboard']);
myApp.controller('MyCtrl',MyCtrl);

function MyCtrl($scope) {
    document.getElementsByTagName('input')[0].focus();
    $scope.$apply();
}