javascript - 如何在不隐藏键盘的情况下清理输入搜索? (移动的)

标签 javascript angularjs ionic-framework

我正在使用 Ionic/AngularJS,这就是我的问题,每次我清理输入时,键盘都会消失,我不希望这样

<ion-header-bar ng-show="searchBoxEnabled">
  <div>
    <input type="search"
           ng-model="query">
    <div ng-show="query"
         ng-click="query=''"> <!--must ONLY clean the input, but is
                                  also disappearing the keyboard-->
    </div>
  </div>
  <!--must clean the input and disappear the keyboard,
      this one is working properly-->
  <div ng-click="hideSearchBox(); query=''">
    |Cancel
  </div>
</ion-header-bar>

在 javascript 方面,我有这几个函数来显示和隐藏输入

$scope.showSearchBox = function() {
  $scope.searchBoxEnabled = true;
};

$scope.hideSearchBox = function() {
  $scope.searchBoxEnabled = false;
};

最佳答案

我同意这可能是输入上的 blur 事件导致键盘消失。

您可以通过按钮上的指令来解决此问题,该指令在单击后重新聚焦于输入(尽管我无法验证这是否会导致键盘闪烁)。

下面是一个说明性示例,您可以在其中传递要重新聚焦的元素的 ID:

app.directive("refocus", function() {
  return {
    restrict: "A",
    link: function(scope, element, attrs) {
      element.on("click", function() {
        var id = attrs.refocus;
        var el = document.getElementById(id);
        el.focus();
      });
    }
  }
});

用法是:

<input id="foo" ng-model="newItem">
<button ng-click="doSomething(newItem)" refocus="foo">add</button>

plunker

关于javascript - 如何在不隐藏键盘的情况下清理输入搜索? (移动的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28465559/

相关文章:

javascript - 如何从另一个angularjs刷新一个 Controller 的计数器值

javascript - Angular js 和 bootstrap 模式选择框

javascript - $ionicplatform 内的 $scope 不起作用

android - 设置 Ionic 应用程序时出错

javascript - Angular - 如何在不使用 id 的情况下从一个页面链接到另一个页面并滚动到 ionic 中的 anchor

javascript - $.when.apply 应用于 Promise 数组

javascript - bootbox.confirm() 无法正常工作

javascript - chrome 71 不会检测 onclick 事件中的 id,除非它是 this.id

javascript - 如何在 VueJS 项目的构建时使用环境变量

php - Symfony2 全栈框架的非事件组件