How to Use Bootstrap to Build Beautiful Angular Apps
How to Use Bootstrap to Build Beautiful Angular Apps
Take a look at this detailed tutorial that demonstrates how to use Bootstrap to build an Angular application from start to finish.
Join the DZone community and get the full member experience.Join For Free
Since 2005, I’ve been a proponent of CSS frameworks. I was even the leader of an open-source project, AppFuse, and during that time, we held a design contest (using Mike Stenhouse’s CSS Framework) in order to provide themes for our users.
Over the next few years, a few new frameworks came into my awareness; Blueprint in 2007 and Compass in 2008. None of these frameworks, however, could hold a candle to Bootstrap. When it first came to my attention, it was called Twitter Bootstrap—invented in 2010 by Twitter employees Mark Otto and Jacob Thorton. To quote their words from "Building Twitter Bootstrap", Issue 324 of A List Apart:
It was in August of 2011 that Bootstrap was released more generally and it swiftly became the most popular GitHub project. Developers everywhere began to use it.
The following year, Angular JS came onto the playing field and took the world by storm. Version 0.9 first appeared on GitHub in October 2010 and version 1.0 was released on June 14, 2012.
In combination, these frameworks had an amazing run. It’s incredible to think that they’ve lasted this long, especially given that both these projects had massive rewrites!
Table of Contents
- Isn’t Angular Dead?
- Angular Still Loves Bootstrap
- Integrate Bootstrap with Angular 9
- Secure Angular and Spring Boot with OpenID Connect
- Use Sass to Customize Bootstrap
- Make Your Angular App Beautiful with Bootstrap
- Fix Bootstrap’s Responsive Menu
- Update the Note List Angular Template
- Add Validation and Bootstrap to the Note Edit Template
- Add a Searchable, Sortable, and Pageable Data Table with Angular and Spring Data JPA
- Add Search by Title with Spring Data JPA
- Add Sort Functionality with Angular and Bootstrap
- Add Sorting and Paging in Spring Boot with Spring Data JPA
- Add Pagination with Angular and Bootstrap
- Angular with Bootstrap + Spring Boot is Powerful
- Learn More About Angular and Spring Boot
Isn’t Angular Dead?
I’ve heard many developers say that Angular is dead. As a veteran Java developer, I’ve heard this said about Java many times over the years as well. Yet it continues to thrive. Angular is similar in that it’s become somewhat boring. Some people call boring frameworks "legacy." Others call them "revenue-generating."
Whatever you want to call it, Angular is far from dead. At least according to the popularity of two recent articles we’ve published! Many of you have flocked to our recent Angular posts—the proof is in the traffic numbers.
- Build a CRUD App with Angular 9 and Spring Boot 2.2
- Build a Beautiful App + Login with Angular Material
Angular Still Loves Bootstrap
You might think that Angular Material is more popular than Bootstrap these days. You might be right, but I believe that who you follow on Twitter shapes your popularity perspective. Most of the fabulous folks that follow me still use Bootstrap.
Integrate Bootstrap with Angular 9
Today I’d like to show you how to integrate Bootstrap into an Angular 9 application. I’ll start with the CRUD example from my aforementioned Angular 9 + Spring Boot 2.2 tutorial. I’ll integrate Bootstrap, convert the app to use Sass (because CSS is more fun with Sass), make the app look good, add form validation, and write some code to develop a searchable, sortable, and pageable data table. The last part sounds hard, but it only requires < 10 lines of code on the Spring Boot side of things. Kotlin and Spring Data JPA FTW!
Clone the previous application into an
In a terminal, navigate into this new directory and its
notes folder. Then install the dependencies for the Angular app.
Add Bootstrap and NG Bootstrap:
|You can leave off the version numbers if you like. However, this tutorial is only guaranteed to work with the versions specified.|
If you run
ng serve -o, you’ll get a blank screen. Look in your browser’s developer console, and you’ll see why.
Uncaught Error: It looks like your application or one of its dependencies is using i18n. Angular 9 introduced a global `$localize()` function that needs to be loaded. Please run `ng add @angular/localize` from the Angular CLI.
Cancel the process and run
ng add @angular/localize to fix this error. Now, if you restart your app, you’ll see it’s pretty simple. And kinda ugly.
Let’s fix that!
styles.css to add a reference to Bootstrap’s CSS file:
app.component.html to use Bootstrap classes.
Now we’re getting somewhere!
To make the login button work, you need to configure both apps' security configurations.
Secure Angular and Spring Boot with OpenID Connect
Using your Okta developer account (you created one, right?), log in to your dashboard, and register your Spring Boot app:
Navigate to Applications > Add Application
Select Web and click Next
Give the application a name and add
http://localhost:8080/login/oauth2/code/oktaas a login redirect URI
okta.env file in the
notes-api directory and copy your settings into it.
Start your Spring Boot app by navigating to the
notes-api directory, sourcing this file, and starting your app.
Then, create a new OIDC app for Angular:
Navigate to Applications > Add Application
Select Single-Page App and click Next
Give the application a name and set the login redirect URI to
auth-routing.module.ts to use your Okta domain and client ID.
Now you should be able to log in with your Okta credentials and create notes to your heart’s content!
You’ll notice there’s some styling, but things aren’t quite beautiful. Yet…
Commit your progress to Git from the
Use Sass to Customize Bootstrap
Before you make things awesome, I’d like to show you how to convert from using CSS with Angular to using Sass. Why? Because Sass is completely compatible with CSS, and it makes CSS more like programming. It also allows you to customize Bootstrap by overriding its variables.
|If you’re not into Sass, you can skip this section. Everything will still work without it.|
First, run the following command in the
notes directory to convert the Angular project to use Sass.
If you run the following
You’ll see three files have a
You can manually rename these to have a
.scss extension, or do it programmatically.
|I had to
Then, replace all references to
angular.json to reference
src/styles.scss (in the
styles.scss to import Bootstrap’s Sass.
To demonstrate how you can override Bootstrap’s variables, create a
src/_variables.scss and override the colors. You can see the default variables in Bootstrap’s GitHub repo.
Then import this file at the top of
You’ll see the colors change after these updates.
Comment out (or remove) the variables in
_variables.scss to revert to Bootstrap’s default colors.
Make Your Angular App Beautiful with Bootstrap
You can see from the screenshots above that
angular-crud generates screens with some styling, but it’s not quite right. Let’s start by adding a Navbar in
app.component.html. Change its HTML to have a collapsible navbar (for mobile devices), add links to useful sites, and add login/logout buttons. While you’re at it, display a message to the user when they aren’t authenticated.
Remove the login and logout buttons from
ng serve and you’ll be able to see your stylish app at
Fix Bootstrap’s Responsive Menu
If you reduce the width of your browser window, you’ll see the menu collapses to take up less real estate.
However, if you click on it, the menu doesn’t expand. To fix that, you need to use the
ngbCollapse directive from NG Bootstrap. Modify
app.component.html to have a click handler on the navbar toggle and add
ngbCollapse to the menu.
app.component.ts and change the
title to be capitalized.
Now, you’ll be able to toggle the menu!
Update the Note List Angular Template
note-list.component.html so the breadcrumb doesn’t float right and all the content is in the same card.
That looks better!
Add Validation and Bootstrap to the Note Edit Template
If you click the New button, you’ll see the form needs some work too. Bootstrap has excellent support for stylish forms using its
note-edit.component.html already uses these classes; you just need to rearrange some things to use the proper
That’s an improvement!
To make the
title field required, add a
required attribute to its
<input> tag, along with a name so it can be referenced in an error message.
Now when you add a new note, it’ll let you know that it requires a title.
If you give it focus and leave, it’ll add a red border around the field.
Add a Searchable, Sortable, and Pageable Data Table with Angular and Spring Data JPA
At the beginning of this tutorial, I said I’d show you how to develop a searchable, sortable, and pageable data table. NG Bootstrap has a complete example I used to build the section below. The major difference is you’ll be using a real server, not a simulated one. Spring Data JPA has some slick features that make this possible, namely its query methods and paging/sorting.
Add Search by Title with Spring Data JPA
Adding search functionality requires the fewest code modifications. Change the
UserController#notes() method in your Spring Boot app to accept a title parameter and return notes with the parameter’s value in their title.
Add the new repository method to the
Restart your server and add a few notes, and you should be able to search for them by title in your Angular app. I love how Spring Data JPA makes this so easy!
Add Sort Functionality with Angular and Bootstrap
To begin, create a
Add it as a declaration in
headers variable to
note-list.component.ts and an
note-filter.ts to have
find() method in
NoteService to pass a
sort parameter when appropriate.
note-list.component.html so it uses the
sortable directive and calls
onSort() when a user clicks it.
Add CSS in
styles.scss to show a sort indicator when a user sorts a column.
Add Sorting and Paging in Spring Boot with Spring Data JPA
On the server, you can use Spring Data’s support for paging and sorting. Add a
Pageable argument to
UserController#notes() and return a
Page instead of a
NotesRepository to add a
Pageable argument to its methods and return a
While you’re updating the Spring Boot side of things, modify
DataInitializer to create a thousand notes for your user. Make sure to replace
<your username> with the email address you use to log in to Okta.
Restart your Spring Boot app to make the data available for searching. Click on the Title column to see sorting in action!
Add Pagination with Angular and Bootstrap
The last feature to add is pagination with NG Bootstrap’s
<ngb-pagination> component. Begin by adding
size variables (with default values) to
At the bottom of
note-list.component.html (just after
</table>), add the pagination component, along with a page-size selector.
NgbModule as an import to
note-list.component.ts, add a
total$ observable and set it from the
search() method. Then add an
onPageChange() method and an
onChange() method, and modify
onSort() to set the page to 0.
notes.service.ts to add a
size$ observable and parameters for the page size and page number.
Now your note list should have a working pagination feature at the bottom. Pretty slick, eh?
Phew! That was a lot of code. I hope this tutorial has helped you see how powerful Angular and Spring Boot with Bootstrap can be! You can find all of the source code for this example on GitHub.
|I fixed the Angular tests so they work with Bootstrap in this commit.|
I also wanted to let you know you can get a lot of this functionality for free with JHipster. It even has Kotlin support. You can generate a Notes CRUD app that uses Angular, Bootstrap, Spring Boot, and Kotlin in just three steps.
Install JHipster and KHipsterShell
easy-notesdirectory and a
notes.jdlfile in itJava
In a terminal, navigate to the
easy-notesdirectory and run
khipster import-jdl notes.jdl
Of course, you probably want to see it running. Run the following commands to start Keycloak (as a local OAuth 2.0 server) and Elasticsearch, and launch the app.
npm run e2e in another terminal window to verify everything works. Here’s a screenshot of the app’s Notes form with validation.
Want to make it work with Okta? See JHipster’s security documentation.
Learn More About Angular and Spring Boot
I used the following resources to gather historical information about Angular and Bootstrap.
If you want to learn more about Angular or Spring Boot, I recommend these blog posts:
This article originally published here.
Opinions expressed by DZone contributors are their own.