Table of Contents
Introduction
In this blog, you’ll be learning the basic implementation of modal window/popup in Angular using Typescript. Angular 2/4 or Typescript are bringing in the true Object Oriented Front-end development. First, we can start with Typescript,
What is Typescript?
Basic Structure of Bootstrap Model in Angular
<!-- open a modal window by clicking button--> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#bootstrapModel">click to open</button> <!-- Modal section --> <div id="bootstrapModel" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Modal Header section</h4> </div> <!-- model body section --> <div class="modal-body"> <p>body text in the modal.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div>
Angular 2 or 4 is a component-based framework with typescript, Components are major building blocks of it. A component can be defined as an independent cohesive block of code that has the required logic, view, and data as one unit.
Bootstrap model in angular typescript
When written using TypeScript, a component is a TypeScript class decorated with @Component() decorator.
Now let us implement the above bootstrap modal window code in angular.
<!-- open a modal window by clicking button basic bootstrap--> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#bootstrapModel"> click to open</button> Now converting it to angular, By clicking the button open the modal using event binding method as the following <button type="button" class="btn btn-info btn-lg" (click)=”openModal()”>click to open</button>
Next, the model window code is implemented as the following :
<div class="backdrop" [ngStyle]="{'display':display}"></div>
<div class="modal" tabindex="-1" role="dialog" [ngStyle]="{'display':display}">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" aria-label="Close" (click)="onCloseHandled()"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Model Title</h4>
</div>
<div class="modal-body">
<p>Model body text</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" (click)="onCloseHandled()" >Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal !--> [ngStyle]="{'display':display}" Display is a defined variable in angular typescript and by default it is none.
display='none';
Next, For opening the model window, click the open modal() and change the display variable value as a block.
openModal(){
this.display='block';
} Same as close the model window,
onCloseHandled(){
this.display='none';
} Finally, we have a nice backdrop to add to the CSS component.
.backdrop{
background-color:rgba(0,0,0,0.6);
position:fixed;
top:0;
left:0;
width:100%;
height:100vh;
} Result
- Angular developer skills.
- How an ideal Angular developer resume should look.
- Top Angular developer tools.
- Most common Angular developer interview questions.
- Present Angular developer job opportunities.
Author: Prasanna R