Using “ng-bootstrap” Components In Angular 5 App

In our last article, we learned “how to install and setup ng-bootstrap in our Angular 5 apps”.

In this article, we are going to cover a couple of bootstrap components provided by “ng-bootstrap” module in our Angular 5 apps.

  • “ng-bootstrap” Checkbox based Buttons (using ngbButton)
  • “ng-bootstrap” Alerts (using ngb-alert)
  • “ng-bootstrap” Progress Bar (using ngb-progressbar)
  • “ng-bootstrap” Carousels (using ngb-carousel)
  • “ng-bootstrap” Rating widget (using ngb-rating)
  • “ng-bootstrap” Modal Popup (using ng-template and ngbModal service)

In order to implement these components, we will have to configure NgbModule in our app module, i.e., app.module.ts file as we have seen in our last article and then we will able to use all ng-bootstrap components in our Angular app.

You can download the complete source code for this Angular ng-bootstrap demo project from here.

Using “ng-bootstrap” Checkbox based Buttons

In order to implement checkbox based buttons, we will have to use “ngbButtonLabel” and “ngbButton” provided by ng-bootstrap module like below.

app.component.ts

  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. @Component({  
  6.     selector: 'app-root',  
  7.     templateUrl: './app.component.html',  
  8.     styleUrls: ['./app.component.css']  
  9. })  
  10. export class AppComponent {  
  11.     title = 'NgBootstrap Directives & Components Demo';  
  12.     classes = {  
  13.         classRoom1: true,  
  14.         classRoom2: false,  
  15.         classRoom3: false,  
  16.     };  
  17. }  

app.component.html

  1. <div style="text-align:center">  
  2.     <h2> {{ title }} </h2>  
  3. </div>  
  4. <div style="margin-top:50px;">  
  5.     <h5>1. ng-bootstrap CheckBox Button Demo</h5>  
  6.     <p>Select the Class-Room you want to know about the number of students.</p>  
  7.     <div class="btn-group" data-toggle="buttons"> <label class="btn btn-primary checkButton" ngbButtonLabel>  
  8.   
  9.         <input type="checkbox" ngbButton [(ngModel)]="classes.classRoom1">Class 1  
  10.   
  11.         </label> <label class="btn btn-primary checkButton" ngbButtonLabel>  
  12.   
  13.         <input type="checkbox" ngbButton [(ngModel)]="classes.classRoom2">Class 2  
  14.   
  15.         </label> <label class="btn btn-primary checkButton" ngbButtonLabel>  
  16.   
  17.         <input type="checkbox" ngbButton [(ngModel)]="classes.classRoom3">  
  18.   
  19.         Class 3  
  20.   
  21.         </label> </div>  
  22.     <p *ngIf="classes.classRoom1">Class Room 1 has 60 students.</p>  
  23.     <p *ngIf="classes.classRoom2">Class Room 1 has 80 students.</p>  
  24.     <p *ngIf="classes.classRoom3">Class Room 1 has 40 students.</p>  
  25.     <hr> </div>  

Output

Output

 

Using “ng-bootstrap” Alerts 
 
In order to implement bootstrap alerts, we will have to use “ngb-alert” provided by ng-bootstrap module like below.

app.component.html

  1. <h5>2. ng-bootstrap Alerts Demo</h5>  
  2. <p>  
  3.     <ngb-alert [dismissible]="false"> This is ng-bootstrap alert in Angular 5 app. </ngb-alert>  
  4. </p>  
  5. <p *ngIf="isAlertDisplayed">  
  6.     <ngb-alert [dismissible]="true" (close)="isAlertDisplayed=false;" type="success">This is ng-bootstrap closable alert in Angular 5 app.</ngb-alert>  
  7. </p>  
  8. <hr>  

ngb-alert – It’s a directive which is used to display a bootstrap alert in our Angular app.

dismissible – It’s a property which is used to display close (cross) button on the right corner of the alert. If set true, close button will be displayed otherwise not.

close – It’s an event which will be fired when close (cross) button will be clicked which is on the right corner of the alert.

type – It’s a property that defines which type of alert will be displayed based on its value like default bootstrap values for alerts like success, warning etc.

app.component.ts

  1. export class AppComponent {  
  2.     title = 'NgBootstrap Directives & Components Demo';  
  3.     isAlertDisplayed = true;  
  4. }  

Output

Output

 

Using “ng-bootstrap” ProgressBar

In order to implement bootstrap progress bars, we will have to use “ngb-progressbar” provided by ng-bootstrap module, like below –

app.component.html 

  1. <h5>3. ng-bootstrap Progress Bar Demo</h5>  
  2. <p style="margin:20px;">  
  3.     <ngb-progressbar type="info" [value]="25">25</ngb-progressbar>  
  4. </p>  
  5. <p style="margin:20px;">  
  6.     <ngb-progressbar type="warning" [value]="counter" [striped]="true" [animated]="true">{{counter}}</ngb-progressbar>  
  7. </p>  
  8. <hr>  

ngb-progressbar – It’s a directive which is used to display a bootstrap progress bar in our Angular app.

type – It’s a property that defines which type of progress bar will be displayed based on its value like default bootstrap values for progress bars like info, warning etc.

value – It’s a property which used to set the progress bar value.

striped – It’s a property which is used to show a progress bar with striped as shown second progress bar in below output window.

animated – It’s a property which is used to show an animated progress bar.

app.component.ts

  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. import {  
  6.     setInterval,  
  7.     clearInterval  
  8. } from 'timers';  
  9. @Component({  
  10.     selector: 'app-root',  
  11.     templateUrl: './app.component.html',  
  12.     styleUrls: ['./app.component.css']  
  13. })  
  14. export class AppComponent implements OnInit {  
  15.     counter = 0;  
  16.     progressInterval;  
  17.     ngOnInit() {  
  18.         this.progressInterval = setInterval(() => {  
  19.             this.counter = this.counter + 10;  
  20.             if (this.counter >= 100) {  
  21.                 clearInterval(this.progressInterval);  
  22.             }  
  23.         }, 200);  
  24.     }  
  25. }  

Output

Output

 

Using “ng-bootstrap” Carousels

In order to implement bootstrap carousel, we will have to use “ngb-carousel” provided by ng-bootstrap module, like below –

app.component.html

  1. <h5>4. ng-bootstrap Carousels Demo</h5>  
  2. <ngb-carousel>  
  3.     <ng-template ngbSlide *ngFor="let img of images"> <img [src]="getImageSrc(img.imageName)" width="100%" height="500px">  
  4.         <div class="carousel-caption">  
  5.             <h3 class="text-center">{{img.name}}</h3>  
  6.         </div>  
  7.     </ng-template>  
  8. </ngb-carousel>  
  9. <hr>  

ngb-carousel – It’s an directive which is used as a container element for the overall carousel. And inside of there, we have defined a number of slides and these slides have been defined by ng-template.

ng-template – It’s a directive which is used as slide with the help of ngbSlide directive.

ngbSlide – It’s a slide directive used by template.

carousel-caption – It’s a class value which is used to  give the caption over the slide.

app.component.ts

  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. import {  
  6.     NgbCarouselConfig  
  7. } from '@ng-bootstrap/ng-bootstrap';  
  8. @Component({  
  9.     selector: 'app-root',  
  10.     templateUrl: './app.component.html',  
  11.     styleUrls: ['./app.component.css'],  
  12.     providers: [NgbCarouselConfig]  
  13. })  
  14. export class AppComponent {  
  15.     constructor(config: NgbCarouselConfig) {  
  16.         config.interval = 2000;  
  17.         config.wrap = true;  
  18.         config.keyboard = true;  
  19.     }  
  20.     images = [{  
  21.         name: "Cup",  
  22.         imageName: "cup.jpeg"  
  23.     }, {  
  24.         name: "Laptop",  
  25.         imageName: "laptop.jpeg"  
  26.     }, {  
  27.         name: "LoveTree",  
  28.         imageName: "love-tree.jpeg"  
  29.     }];  
  30.     getImageSrc(imageN) {  
  31.         return `assets/${imageN}`;  
  32.     }  
  33. }  

Output

Output

 

Using “ng-bootstrap” Rating Widget

In order to implement bootstrap rating widget, we will have to use “ngb-rating” provided by ng-bootstrap module, like below –

app.component.html

  1. <h5>5. ng-bootstrap Ratings Demo</h5>  
  2. <ngb-rating [(rate)]="rateValue" (hover)="hoverValue=$event"></ngb-rating>  
  3. <p>Rating value is {{rateValue}}</p>  
  4. <p>Hover Rating value is {{hoverValue}}</p>  
  5. <hr>  

ngb-rating– It’s a directive which is used to show some rating based functionality for any Angular app provided by ng-bootstrap module.

rate – It’s a property which accepts aa numeric value that is going to be show the correct rating on the app.

app.component.ts

  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. @Component({  
  6.     selector: 'app-root',  
  7.     templateUrl: './app.component.html',  
  8.     styleUrls: ['./app.component.css']  
  9. })  
  10. export class AppComponent {  
  11.     rateValue = 5;  
  12. }  

Output

Output

 

Using “ng-bootstrap” Modal Popup

In order to implement bootstrap modal popup, we will have to use NgbModal service and ng-template provided by ng-bootstrap module, like below.

app.component.html

  1. <h5>6. ng-bootstrap Modal Demo</h5>  
  2. <ng-template #content let-cl="close" let-di="dismiss">  
  3.     <div class="modal-content">  
  4.         <div class="modal-header">  
  5.             <h5 class="modal-title">Modal title</h5> <button type="button" class="close" (click)="cl('Closed from Close button')">  
  6.   
  7.             <span aria-hidden="true">×</span>  
  8.   
  9.           </button> </div>  
  10.         <div class="modal-body">  
  11.             <p>Modal body text goes here.</p>  
  12.         </div>  
  13.         <div class="modal-footer"> <button type="button" class="btn btn-secondary" (click)="di('Dismissed from dismiss button')">Dismiss</button> <button type="button" class="btn btn-primary" (click)="cl('Closed from Close button')">Close</button> </div>  
  14.     </div>  
  15. </ng-template> <button (click)="showModal(content)">Show Bootstrap Modal</button>  
  16. <hr>  

ng-template– It’s a directive which is used to display bootstrap modal content.

#content – It’s a template reference variable which is able to pass the template to modal service.

let-cl – This is a way to stop modal popup being displayed. That means it is used to close the modal popup.

let-di – This is another way to stop modal popup being displayed. That means it is used to dismiss the modal popup.

app.component.ts

  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. import {  
  6.     NgbModal,  
  7.     ModalDismissReasons  
  8. } from '@ng-bootstrap/ng-bootstrap';  
  9. @Component({  
  10.     selector: 'app-root',  
  11.     templateUrl: './app.component.html',  
  12.     styleUrls: ['./app.component.css']  
  13. })  
  14. export class AppComponent {  
  15.     constructor(private modalService: NgbModal) {}  
  16.     showModal(content) {  
  17.         this.modalService.open(content).result.then(  
  18.             (closeResult) => {  
  19.                 //modal close  
  20.                 console.log("modal closed : ", closeResult);  
  21.             }, (dismissReason) => {  
  22.                 //modal Dismiss  
  23.                 if (dismissReason == ModalDismissReasons.ESC) {  
  24.                     console.log("modal dismissed when used pressed ESC button");  
  25.                 } else if (dismissReason == ModalDismissReasons.BACKDROP_CLICK) {  
  26.                     console.log("modal dismissed when used pressed backdrop");  
  27.                 } else {  
  28.                     console.log(dismissReason);  
  29.                 }  
  30.             })  
  31.     }  
  32. }  

Output

Output

Summary

In this article, we covered a couple of ng-bootstrap directives and learned how to use them in Angular 5 apps.

Write to me in the comment box in case you need any help or you have any questions or concerns. Have a good day!


Similar Articles