javascript - 选择一次后,Ionicpopup 自动光标就会消失

标签 javascript angularjs ionic-framework

我在代码中使用 ionic 弹出窗口,其中创建了一个指令来使自动光标聚焦输入框。第一次打开弹出窗口时它工作正常,但光标没有聚焦到当我关闭弹出窗口并再次打开它时,输入框再次出现,任何人都可以告诉我为什么会发生这种情况。

代码:

$scope.data = {};
  var myPopup = $ionicPopup.show({
    template: '<input focus-me type="text" ng-model="data.expensetype" limit-char limit="15">',
    title: $translate.instant('{{"pentercoconuttype_message" | translate}}'),
    scope: $scope,
    buttons: [
     { text: $translate.instant('{{"pcancel_message" | translate}}') },
      {
        text: $translate.instant('{{"psave_message" | translate}}'),
        type: 'button-positive',
        onTap: function (e) {
          if (!$scope.data.expensetype) {
            //don't allow the user to close unless he enters producttype
            e.preventDefault();

          } else {
            addExpenseCategory();
            return $scope.data.expensetype;
          }
        }
      },
    ]
  });
  myPopup.then(function (res) {
    $log.log('Tapped!', res);
    myPopup.close();
  });

指令:

.directive('focusMe', function($timeout) {
return {
link: function(scope, element, attrs) {

  $timeout(function() {
    element[0].focus(); 
  });
}
};
});

最佳答案

只需在 $timeout 中添加 150 毫秒即可确保 View 完全渲染。

.directive('focusMe', function($timeout) {
return {
link: function(scope, element, attrs) {

$timeout(function() {
  element[0].focus(); 
 }, 150);
}
};
});

关于javascript - 选择一次后,Ionicpopup 自动光标就会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46117437/

相关文章:

javascript - RxJs6 - 静态合并可观察数组

javascript - 为div附加点击事件

javascript - 如何在 AngularJS 中将 Controller 绑定(bind)到动态生成的 HTML 页面?

javascript - 取消 ng-change 事件

angularjs - 从 Controller 触发模式弹出

ionic-framework - 无法显示来自 FILE URI、Ionic 框架的图像

javascript - 触发单击 Edge 中的文件输入时不会触发更改事件

javascript - 如何在不使用 DOM/ActiveXObject 的情况下读取带有 javascript/ionic 属性的 xml 标签之间的数据?

javascript - 如何在 ionic 应用程序中显示整页图像

javascript - 如何在 typescript 中安全地声明嵌套对象类型