在使用javascript聚焦后立即在Android上输入松散焦点

Sor*_*iro 5 javascript android angularjs ionic-framework

我正在使用AngularJS开发Ionic App.

我得到了很少的输入(登录/注册的东西),当按下Enter键时 - 在Android上的按钮和iOS上的等效按钮 - 也就是说KeyCode 13,它通过自定义指令专注于下一个输入.

它在网络浏览器上运行得非常好,但在手机上,它会聚焦下一个输入,然后焦点会立即丢失,键盘会自行隐藏,迫使用户再次点击输入.

这是HTML:

<ion-view title="First Screen" hide-nav-bar="true" id="firstScreen" class=" ">
<ion-content padding="false" style="background: url(img/XAYpMMdUTvWFVAnrEhUi_restaurant.jpg) no-repeat center;background-size:cover;" class=" manual-remove-top-padding" scroll="false">
    <div class="">
        <img src="img/awdYDbeeSlSHCToXN5Lw_logo.png" width="auto" height="30%" style="display: block; margin-left: auto; margin-right: auto;">
    </div>
        <label class="item item-input " id="emailInput" name="email">
            <input class="invisible-input" type="email" placeholder="Email" ng-model="user.email" focus-me="currentInput == 0" ng-keyup="inputKeyEvent($event, 0)">
     </label>
    <label class="item item-input " id="passwordInput" name="password">
        <input class="invisible-input" type="password" placeholder="Mot de passe"  focus-me="currentInput == 1" ng-model="user.password" ng-keyup="inputKeyEvent($event, 1)">
    </label>
    <label  ng-show="isSignUp" class="item item-input " ng-class="{'animated fadeInLeft': isSignUp, 'animated fadeOutLeft' : !isSignUp && changed }" id="confirmPasswordInput" name="password">
        <input class="invisible-input" type="password" placeholder="Confirmation du mot de passe"  focus-me="currentInput == 2" ng-model="user.confirmation" ng-keyup="inputKeyEvent($event, 2)">
    </label>
    <div>
        {{response}}
    </div>
    <div class="actionButtonContainer">
        <button ng-click="signin()" id="loginButton" class=" button button-balanced  button-full">Se oonnecter</button>
        <button ng-click="signup()" id="signupButton" class=" button button-energized  button-full" ng-disabled="disableSignUp()">S'inscrire</button>
    </div>
</ion-content>
Run Code Online (Sandbox Code Playgroud)

这是指令:

directive('focusMe', function() {
return {
    link: function (scope, element, attrs) {
        scope.$watch(attrs.focusMe, function (value) {
            if (value === true) {
                element[0].focus();
            }
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

以下是涉及的控制器方法:

$scope.inputKeyEvent = function(event, inputId)
{
    $scope.response = event.keyCode;
    if (event.keyCode == 13)
    {
        $scope.currentInput = inputId + 1;
        if ($scope.currentInput == 2 && !$scope.isSignUp)
            $scope.signin();
        else if ($scope.currentInput == 3 && $scope.isSignUp)
            $scope.signup();
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助 !

编辑

问题是我的genymotion模拟器,它确实在真正的手机上工作(至少我的.)

atc*_*atc 2

我认为你应该blur()在未聚焦的元素上调用方法。我已经用 ionic 和 cordova 完成了它,效果非常好。

focus()另外,将下一个元素包装在 a 中timeout$timeout对于角度版本或setTimeoutvanillajs),以便焦点将在另一个范围内并在模糊完成后执行。

你应该有这样的东西:

$scope.inputKeyEvent = function(event, id) {
    // prevent default 
    event.preventDefault();

    // blur on the unfocused element
    event.target.blur();

    // focus on next element
    // wrapped in timeout so it will run
    // in "another thread" 
    $timeout(function() {
      // If you want to follow all angular best practices
      // Make a broadcast and read the event in the link function
      // of your directive
      var obj = document.getElementById(nextId);

      // Check if object exists
      if(obj) {
       obj.focus();
      }

    });
}
Run Code Online (Sandbox Code Playgroud)