Yesterday, I wanted to add a simple Bootstrap Navigation bar to my Angular application that I created with JHipster. Although this is a standard component, I had difficulties because I didn’t understand how Bootstrap, Angular and NodJS play together. This article explains how to “really” add Bootstrap to an Angular application that got created by JHipster.
Bootstrap comes with Angular. Kind of.
After generating a fresh project with JHipster, Bootstrap styling is already available:
This can be seen when using some Bootstrap styling in main.component.html:
This table is styled correctly:
After using those styles throughout my application, I thought that I could use all Bootstrap functionality. Adding a navigation bar was also fairly easy and looked OK:
However, it didn’t work. When clicking either on the burger button (with a minimized browser window) or the dropdown button, nothing happened.
Only CSS Styling is Usable – Not Java Script
I learned that I had to manually add the NodeJS- and Bootstrap scripts to my index.html:
It’s important to have the NodeJS scripts before the Bootstrap scripts.
After adding those lines, the burger button worked:
Bootstrap vs Angular vs JQuery
In my search for this solution, I found a few articles that made me think about the web ecosystem. Seemingly, there are quite a few approaches to using Bootstrap with Angular.
According to this article, only UI Bootstrap should be used with Angular, under no circumstances should JQuery be added to Angular projects because JQuery “fights directly with how you use Angular to manipulate your views”. This article basically says that my approach is exactly plain wrong. I will have a look into this, but for this article it’s important to have a working solution and to document the reason why Bootstrap seems to be added in the Angular project but the scripted components don’t work.
Ng Bootstrap is a library especially designed to provide directives for Angular and kind of wrap the Bootstrap functionality.
The solution described in this article, adding the full Bootstrap library to Angular, is the way a fellow software craftsman solves the same issue in his own project.
Pace of the Web Ecosystem
I don’t have problems with ambiguousness in regard to solutions of software engineering problems. In the ecosystem of web frontends however, there seem to exist especially numerous solutions to a problem that don’t offer a unique selling point. I could implement every one of the three mentioned solutions and end up with a very similar result. As if the many possible solutions are making the situation not challenging enough, it’s very hard to find documentation because the libraries release new versions very often.
However, this is a topic for another article or – quite possible – not a topic at all because I’m only just beginning to (again) develop web applications and don’t understand the true meaning of the ecosystem.
When adding Bootstrap to an Angular application that got generated with JHipster, the script-files have to be manually added to the index.html because Angular only brings the Bootstrap styles, not the Java Script functionality.