Creating a Modal
In this lesson, we will create a modal with Bootstrap.
We'll cover the following
We have one more page in the application that we’ll add some content to. The contact page should have a form where, if submitted, a modal will pop up. We can use Bootstrap to help us with styling the form and modal. You can learn about the classes required to create a modal here.
Creating the modal component
We’ll create a component to house the modal. In the command line, run the following command:
ng generate component contact/modal
We’ll modify the template file to contain the modal. In the modal.component.html
file, add the following:
<div class="modal show">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Form Submitted</h5>
<button type="button" class="close"><span>×</span></button>
</div>
<div class="modal-body">
<p>We'll responsd ASAP!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary">Close</button>
</div>
</div>
</div>
</div>
<div class="modal-backdrop fade show"></div>
There isn’t anything special about this template. There aren’t any bindings at the moment. We’ll be adding them in as we develop the modal. Next, we’ll want to load the modal.
In the contact-main.component.html
file, add the following:
<form>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>Subject</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<app-modal></app-modal>
Here, we have a form. At the very bottom, we’re loading the modal. It won’t appear because Bootstrap’s CSS will hide it. Let’s override the styles to show the modal. In the modal.component.css
file, add the following:
.modal.show {
display: block;
}
.modal-backdrop.show {
opacity: .5;
}
We’re finished prepping the page. Below is the updated code. If we navigate to the contact page, we’ll see the modal immediately. In the next couple of lessons, we’ll start making it more functional for reusability.
Get hands-on with 1200+ tech skills courses.