【问题标题】:Angular UI Bootstrap Popover - How add a close buttonAngular UI Bootstrap Popover - 如何添加关闭按钮
【发布时间】:2015-10-24 12:58:25
【问题描述】:

我有一个表格,每个单元格都有一个弹出框,如下例所示:

对弹出框的调用:

<td ng-repeat="i in c.installments" ng-class="{ 'first' : i.first, 'last' : i.last, 'advance' : i.advance.value > 0, 'edited' : i.edited, 'final-installment' : i.last }" popover-trigger="{{ popoverFilter(i) }}" popover-placement="top" popover-title="{{i.id == 0 ? 'Advance' : 'Installment ' + i.id}}" popover-append-to-body="true" popover-template="popoverTemplate(i)" ng-init="payment= i; newpayment= i.amount.rounded_value" >

弹出框模板:

<script type="text/ng-template" id="editPopoverTemplate.html">
    <form name="editPayment">
      <h2>{{payment.amount.value|currency:undefined:cents}}</h2>
      <div class="form-group" ng-class="{ 'has-error' : editPayment.newpayment.$invalid }">
        <label>New value:</label>
        <input type="number" name="newpayment" ng-model="newpayment" class="form-control no-spinner" step="1" min="10" required>
        <span ng-messages="editPayment.newpayment.$error" class="help-block" role="alert">
          <span ng-message="required">The value is mandatory</span>
          <span ng-message="min">The value is too low</span>
          <span ng-message="max">The value is too hight</span>
        </span>
      </div>
      <div class="btn-group btn-group-justified" role="group">
        <div class="btn-group" role="group">
          <button class="btn" type="button">Cancel</button>
        </div>
        <div class="btn-group" role="group">
          <button class="btn btn-primary" type="button" ng-disabled="editPayment.$invalid">Save</button>
        </div>
      </div>
    </form>
  </script>

working example on plunker

我需要通过弹出框内的“取消”按钮关闭弹出框。 这是可能的?我需要扩展 Angular UI Bootstrap 库来做到这一点吗?

感谢任何帮助。

当用户在弹出框内部或弹出框外部单击时,链接答案中建议的解决方案会关闭弹出框,但我需要通过弹出框内的“关闭”按钮将其关闭。

【问题讨论】:

标签: angularjs angular-ui-bootstrap popover


【解决方案1】:

使用新的popover-is-open 属性的正确解决方案(如下面的@icfantv 所述)允许使用控制器范围。我在Codepen 中放了一个实例,它是这样的:

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

app.controller(
  'myPopoverCtrl', ['$scope',
    function($scope) {

      // query popover
      $scope.myPopover = {

        isOpen: false,

        templateUrl: 'myPopoverTemplate.html',

        open: function open() {
          $scope.myPopover.isOpen = true;
          $scope.myPopover.data = 'Hello!';
        },

        close: function close() {
          $scope.myPopover.isOpen = false;
        }
      };

    }

  ]);
<head>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.4/ui-bootstrap-tpls.min.js">
  </script>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

</head>

<body 
      ng-app="ui.bootstrap.demo" 
      class="container">

  <button 
          class="btn btn-danger" 
          ng-controller="myPopoverCtrl" 
          popover-template="myPopover.templateUrl" 
          popover-title="This is a popover" 
          popover-placement="bottom" 
          popover-is-open="myPopover.isOpen" 
          ng-click="myPopover.open()">Click me!</button>

  <script type="text/ng-template" 
          id="myPopoverTemplate.html">
    <h2 ng-bind="myPopover.data" />
    <button class="btn btn-success" 
            ng-click="myPopover.close()">Close me!</button>

  </script>

</body>

原答案:

我在这个问题上花了两天时间,终于想出了一个足够简单的 hack。这发生在我的控制器上:

 $scope.close = function(e) {
     el = angular.element(e.target).closest("td"); // `td` is the parent of my clickable
                                                   // element, in this case a `span`
     $timeout(function() { // need $timeout so we don't conflict with the digest loop
     el.children(":first").trigger('close'); // couldn't select the `span` element directly
     });
 },

现在我们在提供者上设置 close 触发器:

app.config(['$tooltipProvider', function($tooltipProvider){
  $tooltipProvider.setTriggers({
    'click': 'close', // Clicks now only open the tooltip, 'close' events close it.
  });
}]);

在我的自定义弹出框 HTML 模板上:

<button type="button" 
        class="btn btn-sm btn-success pull-right" 
        ng-click="close($event)">Close</button>

瞧!我现在可以通过按钮关闭弹出框了!

【讨论】:

  • 非常感谢您发布您的解决方案。我很快会在 plunker 上尝试一下,我也有兴趣看到 icfantv 建议的新属性 popover-is-open 的使用。
  • 如果您选择更新到最新版本的 UI Bootstrap,这将不起作用,因为他们似乎已经放弃了对定制触发器的支持。
  • @dmvianna 这已在 master 中修复。
  • 我还没有尝试过这些解决方案。您能否在使用 popover-is-open 属性制作的 plunker 上发布一个小实例?
  • @icfantv,请随意将此示例用作 angular-ui-bootstrap 的文档。
【解决方案2】:

此解决方案通过弹出框范围的isOpen 字段用于多个ng-repeat 弹出框。

angular.module('ui.bootstrap.demo', ['ui.bootstrap']).controller('PopoverDemoCtrl', function ($scope) {  
  $scope.template = 'myPopoverTemplate.html';
  $scope.close = function(e) {    
    angular.element(e.target).parent().parent().parent().parent().scope().$parent.isOpen = false;
  }
});
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<body ng-app="ui.bootstrap.demo">
<div ng-controller="PopoverDemoCtrl">
    <button ng-repeat="item in ['First Popover','Second Popover','Third Popover']" popover-placement='bottom' uib-popover-template="template" popover-title="{{item}}" type="button" class="btn btn-default">{{item}}</button>  
    <script type="text/ng-template" id="myPopoverTemplate.html">        
        <div class="form-group">
          <button class='btn btn-danger' ng-click='close($event)'>Close Me</button>
        </div>
    </script>
</div>
</body>

【讨论】:

  • 这并不优雅,但它对我来说非常适合使用带有 ng-repeat 的模板
【解决方案3】:

从 Angular UI Bootstrap 0.13.4 版开始,我们添加了通过 tooltip-is-openpopover-is-open 布尔属性以编程方式关闭工具提示和弹出框的功能。

【讨论】:

  • 好消息,一个问题,我看到 Bower 或通过下载可用的稳定版本目前是 0.13.3。 0.13.4 版什么时候会稳定发布?
  • 我看到最后一个角度 ui 引导标记是 0.13.3,但是主分支报告在他的 package.json 上报告了数字版本 0.13.4-SNAPSHOT 我想它现在正在开发中,不是吗?
  • 请不要在多个线程上问同一个问题。目前,计划在 2015 年 9 月 3 日星期四发布 0.13.4。
猜你喜欢
  • 2015-04-02
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
相关资源
最近更新 更多