下拉关闭焦点(dropdown closes on focus)

2019-10-21 10:06发布

我想触发下拉焦点输入场开阔。 但它openes突然关闭每次我集中时间。 随着按钮切换一切正常。 下面是例子: http://codepen.io/piernik/pen/ByKRwE

这里是HTML

<div class="input-group">
    <input type="text" class="form-control" ng-focus="show($event)">

    <div class="input-group-addon"><i class="fa fa-fw fa-calendar"></i></div>
</div>
<a class="btn btn-default" ng-click="toggleDropdown($event)">toggle</a>

<div class="dropdown" dropdown is-open="model.open">
    <ul class="dropdown-menu" role="menu">
        Content
    </ul>
</div>

和JS:

$scope.model = {
                open: false
            };
            $scope.toggleDropdown = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();
                $scope.model.open = !$scope.model.open;
            };
            $scope.show = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();
                $scope.model.open = true;
            };
            $scope.hide = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();
                $scope.model.open = false;
            };

Answer 1:

这是因为发生click事件也是在你的输入触发,防止其默认行为。

DEMO

JAVASCRIPT

angular.module('demo', ['ui.bootstrap'])

  .controller('DemoController', function($scope) {

    $scope.open = false;

    $scope.prevent = function($event) {
      $event.stopPropagation();
      $event.preventDefault();
    };

    $scope.focus = function($event) {
      $scope.prevent($event);
      $scope.open = true;
    };

    $scope.toggle = function($event) {
      $scope.prevent($event);
      $scope.open = !$scope.open;
    }

  });

HTML

  <input type="text" check-if-lost-focus ng-click="prevent($event)"  ng-model="name" name="name" ng-focus="focus($event)">
    <div class="btn btn-default" ng-click="toggle($event)">Toggle</div>

    <div class="dropdown" dropdown is-open="open">
      <ul class="dropdown-menu" role="menu">
          Content
      </ul>
  </div>


文章来源: dropdown closes on focus