如何关闭在ng重复中创建的弹出窗口

时间:2018-01-24 11:25:54

标签: angularjs twitter-bootstrap angular-ui-bootstrap bootstrap-popover

使用angular ui bootstrap我正在创建带有ng重复的模态。我在一个弹药中放了一个小例子。

https://plnkr.co/edit/lpaArn6ewYIbIMjHBb2s?p=preview

我正在试图弄清楚如何让弹出窗口彼此独立地打开和关闭,现在它们全部打开和关闭。

<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="PopoverDemoCtrl">
    <div style=padding-top:200px;"></div>





    <button ng-repeat = "item in [1,2,3]"
    uib-popover-template="dynamicPopover.templateUrl" 
    popover-title="{{dynamicPopover.title}}" 
    popover-is-open="dynamicPopover.isOpen"
    type="button" 
    class="btn btn-default">
    Popover With Template
    </button>

    <script type="text/ng-template" id="myPopoverTemplate.html">
        <div>{{dynamicPopover.content.header}}</div>
        <button ng-click="dynamicPopover.isOpen = !dynamicPopover.isOpen">close</div>

    </script>

</div>
  </body>
</html>

JS

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('PopoverDemoCtrl', function ($scope, $sce) {

  $scope.content = {
    header: 'hello world'
  };

  $scope.dynamicPopover = {
    content: $scope.content,
    templateUrl: 'myPopoverTemplate.html',
    title: 'Title',
    isOpen: false
  };


});

1 个答案:

答案 0 :(得分:1)

您将每个弹出框的状态存储在单个属性dynamicPopover.isOpen中,但您必须单独存储每个弹出框的状态。在您的示例中,您可以将其存储在isOpen: []数组中:

&#13;
&#13;
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('PopoverDemoCtrl', function ($scope, $sce) {
  
  $scope.content = {
    header: 'hello world'
  };
  
  $scope.dynamicPopover = {
    content: $scope.content,
    templateUrl: 'myPopoverTemplate.html',
    title: 'Title',
    isOpen: []
  };

 
});
&#13;
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div ng-controller="PopoverDemoCtrl">
    <div style=padding-top:200px;"></div>
    
    <button ng-repeat="item in [1,2,3]"
            uib-popover-template="dynamicPopover.templateUrl"
            popover-title="{{dynamicPopover.title}}"
            popover-is-open="dynamicPopover.isOpen[$index]"
            type="button"
            class="btn btn-default">
        Popover With Template
    </button>

    <script type="text/ng-template" id="myPopoverTemplate.html">
        <div>{{dynamicPopover.content.header}}</div>
        <button ng-click="dynamicPopover.isOpen[$index] = !dynamicPopover.isOpen[$index]">close</div>

    </script>

</div>
</body>
</html>
&#13;
&#13;
&#13;