【问题标题】:AngularJS - Closing bootstrap popover on button click from within the popoverAngularJS - 在弹出窗口中单击按钮时关闭引导弹出窗口
【发布时间】:2015-10-09 17:03:38
【问题描述】:

正如 plunker http://plnkr.co/edit/O1TK12kJT8vnLKPPILc0?p=preview 所展示的那样。我想在单击Cancel 按钮时显示引导弹出窗口。第一次单击按钮时,它会打开弹出框,但之后我必须单击按钮两次才能打开弹出框。有什么想法吗?

【问题讨论】:

  • 为什么需要实现自己的 popover 指令而不使用此处可用的指令:angular-ui.github.io/bootstrap/?它可以选择通过 url 使用模板,也可以关闭你需要的方式。
  • 其实是第一次打开,第二次点击后……你自己试试吧!

标签: angularjs twitter-bootstrap-3


【解决方案1】:

您的代码中的问题是由于 popover 具有的切换功能。所以在popover关闭后,第一次点击它实际上是去掉了toggle,第二次已经打开popover了。

解决方案(基于您的 plunkr):

将控制器和指令更改为上述:

app.controller('AppController', function($scope) {
 $scope.cancel = function(msg) {
  $scope.msg = msg;
 };
});

app.directive('cancelConfirmation', function($compile, $timeout) {
return {
  restrict: 'A',
  scope: {
    onCancel: '&'
  },
  link: function(scope, elem, attrs) {

      var content = '<div><p>Are you sure you want to cancel?</p><button id="yes" class="btn btn-default" ng-click="no()">No</button>&nbsp;' +
        '<button id="no" class="btn btn-primary" ng-click="yes()">Yes</button></div>&nbsp;';

      elem.popover({
        html: true,
        content: $compile(content)(scope),
        placement: 'top'
      });

      elem.on('shown.bs.popover', function() {
        elem.attr('disabled', true);
      });

      scope.yes = function() {
        elem.popover('hide');
        elem.triggerHandler('click');
        elem.attr('disabled', false);
        scope.onCancel()('yes called');
      };

      scope.no = function() {
        elem.popover('hide');
        elem.triggerHandler('click');
        elem.attr('disabled', false);
      };
  }
};
});

在您的取消按钮上:

<button class="btn btn-danger" cancel-confirmation data-on-cancel="cancel">Cancel</button>

【讨论】:

    【解决方案2】:

    我可能有一个解决方法:

    elem.popover({
       html: true,
       content: $compile(content)(scope),
       placement: 'top',
       trigger: 'manual'
    });
    
    elem.on('click', function() {
       elem.popover('show');
    });
    

    这样,如果显示弹出框并在元素上添加点击侦听器,您必须手动触发。

    【讨论】:

      【解决方案3】:

      看看this为你解决方案。

      得到这个的步骤:

      1.从脚本中删除指令

      2.add bootstrap-tpls.js from here 并将其包含在您的页眉中

      3.更改脚本以使应用程序具有注入的引导程序和控制器:

      var app = angular.module('myApp', ['ui.bootstrap']);
      
      app.controller('AppController', function($scope) {
        $scope.yes = function() {
            console.log('yes called');
          };
      
          $scope.no = function() {
            console.log('no called');
          };
      });
      

      4.使用以下标记创建yes-no.html 模板:

       <div><button id="yes" class="btn btn-default" ng-click="no()">No</button>&nbsp;
       <button id="no" class="btn btn-primary" ng-click="yes()">Yes</button></div>&nbsp;
      

      5.将取消按钮更改为

       <button popover-trigger="focus" uib-popover-template="'yes-no.html'" popover-title="Some title here?" type="button" class="btn btn-danger">Cancel</button>
      

      享受更简单的解决方案。

      【讨论】:

      • 感谢@Diana 的解决方案。我知道ui-bootstrap 为它提供了一个指令。但是我试图了解我的代码中缺少什么,如果我们只使用引导程序的弹出窗口?是因为元素与 DOM 分离了吗?
      • @AnupVasudeva 在您的示例中,您没有任何与将关闭它的弹出框的焦点丢失相关的事件。这就是缺少的。
      • 我只想在 No/Yes 按钮上关闭弹出框,而不是在模糊事件上。
      • @AnupVasudeva 哦,现在我明白了。问题在于按钮用作切换按钮。所以在弹出窗口关闭后,当你点击取消时,它实际上会取消按钮,第二次只会再次打开弹出窗口。
      • 正确。你说的对。您的解决方案使我的代码正常工作。现在,如果您想在下面添加您提到我的建议的新答案,以便我可以标记它。这是更新的 plunk:plnkr.co/edit/zGliZU9iR4eN2g9iTIdX
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2012-12-21
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多