【问题标题】:angularJS focus in bootstrap modal when it is opened when page loadangularJS在页面加载时打开时聚焦在引导模式中
【发布时间】:2015-01-27 21:06:48
【问题描述】:

在我的应用程序中(是的,我知道这有点糟糕)在控制器中有这样的代码:

  $scope.changePasswordModal = function() {
    angular.element('#password-change').modal('show');
  };

这段代码是这样出现的:

  if(angular.isDefined($routeParams.chgkey)){
    $scope.changePasswordModal();
  }

我有这样的看法:

<div class="modal custom" id="new-password-remember" tabindex="-1" role="dialog" aria-labelledby="newPasswordRemember" aria-hidden="true" modal-center modal-auto-focus modal-auto-focus-type="1" modal-auto-focus-action="rememberpwd" data-backdrop="static">
....

我在这个视图上还有一些其他的模态形式:当我点击打开模态时 - 一切正常:显示焦点。但是当我打开页面并通过操作在控制器中打开它时,我看到它有焦点,但为什么不显示这个焦点?

指令:

.directive('modalAutoFocus', ['$timeout', '$window', function ($timeout, $window){
    return {
      restrict: 'EA',
      link: function(scope, element, attrs) {


        if (attrs.id == 'new-password-change'){
          element.find('.on-focus')[0].focus();
        }

        $(element).on('shown.bs.modal', function(){
          element.find('.on-focus')[0].focus();
        });
    }
  }
  }]);

我不明白什么是不好的...为什么不显示焦点?我做错了什么?

谢谢

【问题讨论】:

  • 带有指令的元素的 id 是 new-user 而不是 new-password-change 是你想要的吗?您的指令是否也在 ID 为 new-password-change 的元素上?
  • @micha 是的,一切都很好,只是我的错误,我更新了问题...

标签: javascript jquery angularjs twitter-bootstrap


【解决方案1】:

我遇到了同样的问题,该代码修复了它:

$timeout(function(){
    element.find('.on-focus')[0].focus();
}, 0);

【讨论】:

  • 如何处理我的指令?
  • 你只需通过上面的代码更改element.find('.on-focus')[0].focus();
  • if (attrs.id == 'new-password-change'){ element.find('.on-focus')[0].focus(); } $(element).on('shown.bs.modal', function(){ element.find('.on-focus')[0].focus(); }); 我有两个...
  • 两者都改,你会有两个$timeout(function(){ element.find('.on-focus')[0].focus(); }, 0);
猜你喜欢
  • 2019-01-01
  • 2015-03-20
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多