Lazy Panda

Developer

Posts
57
Comments
48
Likes
76
Posts
57
Comments
48
Likes
76
sidebar

How to integrate Bootstrap Popover in Angular 13 application

Popovers are a very useful component with a small overlay of content that is used for displaying secondary information of any element when the user either clicks or hovers on any HTML element. In this tutorial, I am going to use the bootstrap 4 Popover component in the Angular 13 based application. 

This tutorial can help to integrate the Bootstrap 4 popover component on any of Angular with the TypeScript applications. 

 

What are those Bootstrap Popovers?

Popovers are a very small overlay of content, showing some useful information to the user. Popovers are floating based on UI viewport. It can be shown by any user action like Click, Hover or it can be shown automatically once the page gets loaded completely. 


What I am going to cover in this article - 

  • Creation of an Angular application
  • Integrate bootstarp 4 framework in Angular Application
  • Initialize Popover component and expose methods to control Popovers from Angular Component

 

angular-bootstrap-popover

 

 


Creation of an Angular application

Let's create an angular application using the below command - 

ng new popover-demo

 

I am not using any routing and using SCSS for setting up the application.


Integrate bootstrap 4 framework in Angular Application

Install below npm library to get bootstrap on your Angular application.

npm install --save-dev bootstrap@4.6.0 jquery popper.js

 

Open the Angular.json file and update the script section with the following - 

"styles": [

"src/styles.scss"

],

"scripts": [

"node_modules/jquery/dist/jquery.min.js",

"node_modules/popper.js/dist/umd/popper.js",

"node_modules/bootstrap/dist/js/bootstrap.min.js"

]

 

 

Then open the style.scss file and the following as well - 

@import '~bootstrap/scss/bootstrap';

 


Initialize Popover component and expose methods to control Popovers from Angular Component

First we are going to declire jquery variable and using that we can access the Popover methods. 

declare const $: any;

After that in ngAfterViewInit() method we can inilize the popover for automatic load. 

ngAfterViewInit() {

setTimeout(() => {

$('#auto-popover-lbl').popover('show');

}, 1000);

 

if (!this.popoverApplied) {

$('[data-toggle="popover"]').popover();

this.popoverApplied = true;

}

}

 

Let's see the HTML part of it - 

<a id='hover-me-lbl'

data-container="body"

data-trigger="hover"

data-toggle="popover"

data-placement="top"

[attr.data-content]="lblPopoverTextValue">

<label>Hover Me!</label>

</a>

 


Demo Time - 

 

So that's all for today. Let's me if you have any questions please comments below.

Happy Coding!

-LP