Skip to content

Instantly share code, notes, and snippets.

@space11
Created April 5, 2024 11:35
Show Gist options
  • Save space11/f894b3b81e871004108c922ac7d37520 to your computer and use it in GitHub Desktop.
Save space11/f894b3b81e871004108c922ac7d37520 to your computer and use it in GitHub Desktop.
Allow only one ngx-bootstrap Popover at the time.
<button
type="button"
class="btn btn-default btn-secondary"
popover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
popoverTitle="Popover on top"
placement="top"
>
Popover on top
</button>
import { Component, ViewChildren, QueryList, AfterViewInit } from '@angular/core';
import { PopoverDirective } from 'ngx-bootstrap/popover';
@Component({
selector: 'demo-popover-four-directions',
templateUrl: './four-directions.html'
})
export class DemoPopoverFourDirectionsComponent implements AfterViewInit{
@ViewChildren(PopoverDirective) popovers: QueryList<PopoverDirective>;
ngAfterViewInit() {
// NOTE: Rember to take care of unsubscribing when component / directive is destroyed
this.popovers.forEach((popover: PopoverDirective) => {
popover.onShown.subscribe(() => {
this.popovers
.filter(p => p !== popover)
.forEach(p => p.hide());
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment