Mat*_*nis 16 javascript twitter-bootstrap angularjs angular-ui-bootstrap angular-bootstrap
当我popover
点击弹出窗口外的任何地方时,我试图关闭我的Angular-bootstrap .根据这个问题的答案,现在可以通过利用新popover-is-open
属性来实现(在版本0.13.4中):在单击外部时隐藏Angular UI Bootstrap popover
目前我的HTML看起来像这样:
<div
ng-click="level.openTogglePopover()"
popover-template="level.changeLevelTemplate"
popover-trigger="none"
popover-placement="right"
popover-is-open="level.togglePopover">
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-sort"></span>
</button>
</div>
Run Code Online (Sandbox Code Playgroud)
......和我的相关控制器代码:
vm.togglePopover = false;
vm.openTogglePopover = function() {
vm.togglePopover = !vm.togglePopover;
};
Run Code Online (Sandbox Code Playgroud)
这对于在单击上面引用的按钮时打开/关闭弹出窗口非常有用.我的问题是,当点击弹出窗口外的任何地方时,我如何扩展此功能以关闭弹出窗口?我如何设置事件处理来完成此任务?
cda*_*uth 13
由于angular-ui 1.0.0,outsideClick
工具提示和弹出窗口有一个新的触发器(在此拉取请求中引入:
<div
uib-popover-template="level.changeLevelTemplate"
popover-trigger="outsideClick"
popover-placement="right">
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-sort"></span>
</button>
</div>
Run Code Online (Sandbox Code Playgroud)
Dzi*_*inX 11
首先,如果您希望弹出窗口在任何单击时关闭,而不仅仅是弹出窗口外的弹出窗口,您可以使用现有的UI-Bootstrap代码来完成:
<button class="btn btn-default btn-xs" type="button"
popover-template="level.changeLevelTemplate"
popover-trigger="focus"
popover-placement="right">
<span class="glyphicon glyphicon-sort"></span>
</button>
Run Code Online (Sandbox Code Playgroud)
这里的诀窍是放下周围的东西<div>
,然后popover-trigger="focus"
右键放在按钮上.
如果您只需要为弹出窗口内容之外的点击关闭popover,那么它将更加困难.你需要一个新的指令,如下所示:
app.directive('clickOutside', function ($parse, $timeout) {
return {
link: function (scope, element, attrs) {
function handler(event) {
if(!$(event.target).closest(element).length) {
scope.$apply(function () {
$parse(attrs.clickOutside)(scope);
});
}
}
$timeout(function () {
// Timeout is to prevent the click handler from immediately
// firing upon opening the popover.
$(document).on("click", handler);
});
scope.$on("$destroy", function () {
$(document).off("click", handler);
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
然后,在弹出模板中,使用最外层元素上的指令:
<div click-outside="level.closePopover()">
... (actual popover content goes here)
</div>
Run Code Online (Sandbox Code Playgroud)
最后,在您的控制器中,实现以下closePopover
功能:
vm.closePopover = function () {
vm.togglePopover = false;
};
Run Code Online (Sandbox Code Playgroud)
我们在这里做的是:
close-popover
指令的元素:
close-popover
这不是最干净的解决方案,因为你必须从弹出模板中调用控制器方法,但这是我想出的最好的.
归档时间: |
|
查看次数: |
15780 次 |
最近记录: |