不能在角度和离子的文本框上触发焦点

Mon*_*key 8 angularjs ionic-framework

我试图通过onclick触发器将焦点设置在文本框上,它似乎不适用于我的离子角度实现.如果我把它从阴道和离子中取出来,它就有效

这是模板

<ion-view title="Create">
  <ion-content class="has-header">
     <div>
        <input type="text" placeholder="focus here" id="test" />
        <button ng-click="test()">TEST</button>
      </div>
  </ion-content>
</ion-view>
Run Code Online (Sandbox Code Playgroud)

和js

$scope.test = function(){
    document.getElementById('test').focus();
  };
Run Code Online (Sandbox Code Playgroud)

Ind*_*dam 11

我在论坛上找到了答案:http: //forum.ionicframework.com/t/focus-method-doesnt-work-on-input-textarea-etc/5606/14

原来你需要添加Ionic Keyboard插件并在config.xml中添加这些首选项:

<preference name="KeyboardDisplayRequiresUserAction" value="false" />

<feature name="Keyboard">
  <param name="ios-package" value="IonicKeyboard" onload="true" />
</feature>
Run Code Online (Sandbox Code Playgroud)

之后,focus()函数将起作用.

  • <preference name ="KeyboardDisplayRequiresUserAction"value ="false"/>这一行帮助了我 (3认同)

Rob*_*Rob 6

我完成此操作的最简单,最快捷的方法如下:

$scope.test = function(){
    $timeout(function () {
        document.getElementById('test').select();
    }, 0);
};
Run Code Online (Sandbox Code Playgroud)