我有一个桌子,每个单元格都有一个popover,如下例所示:
对popover的电话:
<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" >
popover模板:
<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>
我需要通过popover中的“取消”按钮关闭popover.
这是可能的?我需要扩展Angular UI Bootstrap库来做到这一点?
任何帮助是赞赏.
在链接的答案中建议的解决方案关闭popover,当用户点击popover内部,或在popover外面,但是我需要通过popover中的“close”按钮关闭它.
使用新的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>
原来的答案:
我花了最后两天这个问题,最后想出了一个简单的黑客.这在我的控制器上
$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 }); },
现在我们在提供商上设置了关闭触发器:
app.config(['$tooltipProvider',function($tooltipProvider){ $tooltipProvider.setTriggers({ 'click': 'close',// Clicks now only open the tooltip,'close' events close it. }); }]);
并在我的自定义popover HTML模板:
<button type="button" class="btn btn-sm btn-success pull-right" ng-click="close($event)">Close</button>
瞧!我现在可以通过按钮关闭popover了!