In light of the recent COVID-19 pandemic - OPTASY would like to offer DRUPAL website support for any Health Care, Government, Education and Non-Profit Organization(s) with critical crisis communication websites or organizations directly providing relief. Stay Safe and Stay Well.

How to Use Bootstrap with Angular 4? Here Are 3 Ways to Add It To Your Project 

How to Use Bootstrap with Angular 4? Here Are 3 Ways to Add It To Your Project 

by Silviu Serdaru on Apr 30 2018

Here you are now: your Angular 4 front-end app ready to... wow its users! “Almost ready” actually! For it still needs styling... And what better HTML and CSS framework to go for than Bootstrap, right? But how to use Bootstrap with Angular 4 more precisely?

How do you properly integrate it into your Angular 4 CLI project?

Great news: you have not just one, but 3 options at hand for adding it!

Let me get into details:
 

On Using Bootstrap in Your Front-End Development Process

Is there any need to list here the reasons why it's precisely Bootstrap that you're planning to implement into your Angular CLI project? Angular 4, to be more specific.

After all, it's the most popular framework for styling websites built in HTML, CSS and modern web & mobile JavaScript frameworks (like Angular here):

It's an open source, feature-rich framework that turns front-end development into a such a “breeze”. Basically, it empowers you to build responsive layouts without the need to be a CSS “expert”.

And now, let's break down further with the step-by-step “tutorial” on how to use Bootstrap with Angular 4:
 

Step 1: Create a New Angular Project Using Angular CLI 

The very first step to take is obviously setting up a brand new project.

Use the Angular Command Line Interface to generate it.

But first, install it to on your system:

$ npm install -g @angular/cli 

It's only then, once you've installed its NPM package, that you can go ahead and... generate your new project. 

For doing this, just type the following command in your CLI:

$ ng new myproject 

Next, feel free to change into that specific directory and to turn on the web server:

  • $ cd myproject
  • $ ng serve 
    
    

“App works!” This is the message that you should be seeing in your browser right now.

 
Step 2: Install Bootstrap to Your Project

Now that you've launched your new Angular project, it's time to add your Bootstrap library, as well.

And you sure aren't nickel and dimed in options. There are 4 ways to add Bootstrap to Angular 4.
 

Step 3: How to Use Bootstrap with Angular 4 — 3 Different Ways to Integrate It

Option 1: Install Bootstrap from CDN

And there are 2 particular files that you'll need to install from CDN into your project:
 

  1. the Bootstrap CCS file
  2. the Bootstrap JavaScript file 
     

Note: keep in mind to add the jQuery JavaScript library file, as well!

Next, open the src/index.html file and insert the following:
 

  • the <link> element to add the Bootstrap CSS file at the end of the head section
  • a <script> element for adding jQuery at the bottom of the body section
  • a <script> element for inserting the Bootstrap JS file at the bottom of the body section
     

Eager to see “Bootstrap in action” in one of your project's component templates? Then give it a try:
 

  • open the src/app/app.component.html
  • enter the following code there:
     
<div class="container">
  <div class="jumbotron">
    <h1>Welcome</h1>
    <h2>Angular & Bootstrap Demo</h2>
  </div>
  <div class="panel panel-primary">
    <div class="panel-heading">Status</div>
    <div class="panel-body">
      <h3>{{title}}</h3>
    </div>
  </div>
</div>

And it's the following message that this HTML template code should trigger in your browser:

“app works!”

Note: go for a Bootstrap theme of your choice; once you've downloaded it (from Bootswatch.com for instance), its bootstrap.min.css file will get instantly opened up in your browser.

Just copy the file's URL and use it to replace the string assigned to the href attribute of the <link> element, in the index.html file.

And voila! It's precisely those colors, defined by your chosen theme, that get displayed in the browser now!
 

Option 2: Install Bootstrap using NPM

And here's another valid answer to your “How to use Bootstrap with Angular 4” dilemma!

Simply enter:

$ npm install [email protected] jquery –save

It's this command that will integrate Bootstrap and jQuery into the node_modules folder of your Angular 4 project directory.

Moreover, it will include these 2 dependencies in the package.json file, as well.

Once properly installed, you can find both packages at:
 

  • node_modules/bootstrap/dist/css/bootstrap.min.css
  • node_modules/bootstrap/dist/js/bootstrap.min.js
  • node_modules/jquery/dist/jquery.min.js
     

Note! You have 2 options for integrating those files into your Angular 4 project:
 

  1. add the file paths to the script array and to the file path of the angular-cli.json file
  2. add the corresponding <script> and <link> elements to your index.html file
     

Option 3: Add NG-Bootstrap to Your Project

The great thing about this method is that you'll no longer need to add jQuery and Bootstrap dependencies. Ng-Bootstrap comes packed with a set of built-in native Angular directives which are already CSS and Bootstrap's markup-based.

Now, getting back to our initial “How to use Bootstrap with Angular 4” question, let's see how we install this NPM package. 

For this, just enter the following command in your Angular 4 project directory:

npm install --save @ng-bootstrap/ng-bootstrap 

Next, make sure you also install Bootstrap 4 to your project:

$ npm install [email protected]

And, the final step is to add the following files:
 

  • jquery.min.js
  • bootstrap.min.js
  • bootstrap.min.css
     

… to your .angular-cli.json file

Now you still need to import the Ng-Bootstrap’s core module — NgbModule — from its @ng-bootstrap/ng-bootstrap package.

To do this, just type the following import statement into app.module.ts:

import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

All there's left for you to do now is to add the NgbModule to the @NgModuledecorator's imports array. 

And since we're here, you'll find some more than “enlightening” info (chunks of code here included!) on the 2 different options at hand for importing the NGBModule:

either in your project's child modules 
or in your the root module itself

… in this article here on Using Bootstrap with Angular.
 

Using The NG-Bootstrap Components: Which Are They? 

With the NgbModule installed into your Angular 4 project, you're now able to use the Ng-Bootstrap components.

To leverage them in your app.component.html.

Speaking of which, here are the components at hand:
 

  • Accordion
  • Alert
  • Rating
  • Tabs
  • Carousel
  • Progressbar
  • Collapse
  • Datepicker
  • Buttons
  • Pagination
  • Typeahead
  • Popover
  • Timepicker
  • Dropdown
  • Modal
  • Tooltip
     

The END! Does this answer your “How to Use Bootstrap with Angular 4” question? 

Which method of adding this front-end framework to your project is more suitable for you?

Development

We do Web development

Go to our Web development page!

Visit page!

Recommended Stories

How Does Using Component-Based Development in Drupal 8 Benefit Your Team More Precisely?
With the Twig templates replacing the old PHP templates, Drupal has been brought to a whole new “era”. We can now leverage the advantages of a component-based development in Drupal 8. But what does that mean, more precisely? How does this (not so) new approach in software development benefit you? Your own team of developers... And everyone's talking about tones of flexibility being unlocked and about the Twig templates' extensibility. About how front-end developers, even those with little knowledge of Drupal, specialized in various languages, can now... “come right on board”. Since they're already familiar with the Twig engine... Also, we can't ignore all the hype around the advantage of the streamlined development cycles in Drupal and of the consistent user experience across a whole portfolio of Drupal apps/websites. But let's take all these tempting advantages of component-based UI development in Drupal 8 and point out how they benefit your team precisely.   1. But First: What Is a Component? It's a standalone piece of software that can appear in multiple places across your Drupal website/application. One of the most relevant examples is that of a content hub. One displaying teasers of the latest blog posts, events... You could set up a component that would determine how each item in that content hub should look like. In short:   one single component can be used by several types of content any update to its template/style would automatically reflect on all those content types, as well   Accessible via an API, this independent piece of software explicitly defines all its application dependencies.| Your team could then easily architect a new interface by just scanning through and selecting from the library of components.   2. What Is Component-Driven Development? What Problems Does It Solve? A succinct definition of component-based software engineering would be: A software development technique where you'd select off-the-shelf, reusable components and put them together according to a pre-defined software architecture. “And what challenges does it address?” It streamlines and lowers the level of complexity of otherwise intricate, time-consuming development and design processes. As the author of given components, your role is to get them implemented. No need to worry about how they'll get “assembled”; this is what the well-defined external structure is there for. Word of caution: mind you don't get too... engrossed in putting together the right components, in architecting the best component-based structure, for you then risk investing too little time in... building them properly.   3. Component-Based Development in Drupal 8 Now, if we are to focus our attention on the component-based UI approach in relation to Drupal 8 software development, here are the key aspects worth outlining:   with the Twig engine in Drupal 8, you're free to “joggle with” extensible templates; once you've defined a Twig template in one place, we get to reuse it across the whole Drupal website/app   the Component Libraries module allows you to set up template files (storing all their needed JS and CS), assign a namespace for them and place them pretty much anywhere on your Drupal filespace (not just in your themes' “templates” directory)   you then get to use the KSS Node library and define a living style guide; it's where you'll store all the component templates built for your Drupal website (styles, markup, JS behaviors, etc.)   By filling in your toolboxes with all these tools — the results of a joint effort of the Drupal and the front-end communities  —  you're empowered to design themes that are more modular. And, therefore, more efficient... 4. The Top 6 Benefits of the Component-Based UI Approach   4.1. It Ensures UX Consistency Across All Your Drupal 8 Websites Take your library of components as the “headquarters” for all the teams involved in your Drupal project: QA, business, development, design teams... It's there that they can find the pre-defined standards they need to keep the consistency of the features they implement or of other tasks they carry out across multiple projects. A consistency that will bubble up to the user experience itself, across your whole portfolio of Drupal 8 websites/applications...   4.2. It Accelerates the Process of Turning Your Visual Design into a UI  Embracing the component-based development in Drupal 8 you'd avoid those unwanted, yet so frequent scenarios where the front-end developer gets tangled up in the wireframe he receives and:   he/she translates parts of it the... wrong way he digs up all types of “surprise” issues     By using a component-driven UI approach translating a visual design into a user interface gets much more... event-less.  With:   a pre-defined component architecture to rely on well-established standards to follow a whole library of component templates at hand   … there are fewer chances of discrepancies between the UX defined in the visual design and the one delivered via the resulting user interface. Not to mention the reduced delivery timelines...   4.3. It Streamlines the Whole Development Process  “Sustainability” is the best word to define this approach to Drupal software development. Just think about it:   whether it's a particular grid, navigation or layout that your front-end developer needs when working on a new project, he/she can pull it right from the component library at hand   … and “inject” it into the app/website that he's working on   in case that element needs further updating, the developer will already have the baseline to start with   … there's no need for new components to be designed, from the ground up, with every single project: the already existing ones can always get further extended   And that can only translate into significant savings of both time and money.   4.4. It Reduces the Time Spent on Setting Up the Functionality &amp; Defining the UX And this is one of the key benefits of using component-based development in Drupal 8. Your various teams would no longer need to define the UX requirements and the functionality every single time during the design process. With an easily accessible library of components, they can always pull a component standing for a specific requirement (display of complex data, filtering, pagination in grids, etc.) and just define its extensions. And the business logic, as well.   4.5. It Enables You to Systematically Reuse Your Components And “reusability” goes hand in hand with “sustainability”. I would even say that it's a synonym for “future-proofing”, as well... Just think about it: by having a Drupal 8 website in a component-based format you can always rearrange components as technologies grow outdated and new ones emerge... In short, embracing a component-based development in Drupal 8 enables you to remove the need of rebuilding your website every time its underlying technologies “grow out of fashion”. With your component library at hand, you'll be able to swap your guidelines, design patterns and various content templates in and out, keeping your Drupal app or website up to date.   4.6. It Integrates Seamlessly into the Development Process  By leveraging a component-based development in Drupal 8, you'd also gain better control over the whole development cycle. The update process here included... Since you'd then build your components and manage your production quality user interface code in a repository like GitHub, every update that you'd make will be displayed in there. And be easily accessible to everyone in your team. In short, your developers get to pull pieces of code from the repository to further extend them, then re-submit them to GitHub (or to another source code repository) for review. With the ability to version your component library, your team can keep a close track of all your Drupal applications with their corresponding versions of the approved UX.   The END! This is how the component-based development in Drupal 8 would benefit you and your team. Have we left out other key advantages of using this approach? Image by Arek Socha from Pixabay ... Read more
Silviu Serdaru / Apr 11'2019
How to Create an Angular Project with Angular CLI in 5 Simple Steps
About to build your very first Angular app? Then you must be planning to create an Angular project with Angular CLI, right? The much-acclaimed tool that the Angular team built precisely to jumpstart the whole development process. … to have a simple app scaffolded, generated and deployed in no time, by entering just a few commands (so they say, at least).  And since I'm sure that you don't want to waste these bundles of convenience by letting yourself tangled up in overly complex explanations instead, I've kept things simple with this guide. So, here's how you create and run your first Angular project via the Angular command-line interface:   1. But How Precisely Can Angular CLI Jumpstart Your App's Development Process? Take this command-line interface as a starter kit “present” that the Angular team has nicely wrapped for you:   it's practically geared at empowering you to get up and start developing a new Angular app in no time it takes just one short command to generate a default Angular project that would include all the needed dependencies (in the node_modules folder), as well as testing files for each component   Now, this is what I call a major kickstart! It's got you covered from the stage of setting everything up, to creating the Angular project itself, to testing it and finally deploying it. Other key usages of the Angular CLI that we're not going to focus on in this tutorial here are:   real-time server maintenance and support building applications for production adding new features to your existing app  running tests on your application units    2. Setting It Up: Install Angular CLI Globally Before you jump to the part where you create an Angular app using Angular CLI, you need to install the command-line interface itself. Globally! And this is the “power” command to enter in your terminal with the npm installed: npm install -g @angular/cli Notes: if you already have the CLI installed, make sure it's the latest version if you need to update it, these are the commands to enter: npm uninstall -g angular-cli npm uninstall --save-dev angular-cli And there's more! A few more must-have dependencies that you need to make sure that are already installed and upgraded to their latest versions: Node.js: v6.9.x + npm: 3.x.x +   3. Create an Angular Project With Angular CLI With your command line interface ON, use it to enter THE one and only command that will generate a new Angular project for you. One incorporating, by default, all the needed dependencies: ng new ng-yourproject Tada! A “yourproject” named directory has just been generated. That's where your new Angular project — along with all the requested dependencies — gets stored. Eager to test it out? Just run the following command in your terminal: ng serve   Your Angular app will then get built and served up to localhost:4200. Feel free to open this URL in your browser and it's the here-below screen that you should be able to see: Basically, it's the default application shell itself rendered by the CLI.   4. “Deconstructing” Your New Angular Project: What Does It Include? Now, before you go ahead and do your “tweaking” on your newly created app, let's see what you've got in there! What are the elements that the CLI has generated for you to help you jump to development right out of the box? For this quick “scan”, open your Angular project in your IDE of choice and start “exploring” your src/folder:   src/*    styles.css any styles that you'll plan to apply globally, it's this file that you can add them to; and it's here, as well, that you can import new .css files (Bootstrap or any other styling frameworks of your choice)   index.html  where your Angular app gets started   src/app/*    app.component.ts this is where your app's one and only (for now at least) component gets stored   app.module.ts  the modules Angular needs for managing your app's components note: @NgModule marks the class file as a module and this is what makes it similar to @Component   5. Create a New Component  Remember your “one and only component” that I mentioned during the previous “inventory” of all the “bunch of stuff” that CLI has generated in your project? Well, how about creating a new one now? One that would load under that root component? Just run the following command to generate it: ng generate component the-quote Next, time to “show it off” in your browser: &lt;h3&gt;{{myQuote.quote}}&lt;/h3&gt; &lt;small&gt;- {{myQuote.by}}&lt;/small&gt; Add the app-the-quote selector to the root component that the CLI generated in your Angular project: &lt;h1&gt; {{title}} &lt;/h1&gt; &lt;app-the-quote&gt;&lt;/app-the-quote&gt; 6. Apply External Styling  Now you do agree that when you create an Angular project with Angular CLI applying styling is a key step. So, let's add your favorite CSS framework to your new application! Me, it's Bulma that I'll be using in this tutorial here: npm install bulma --save With our CSS framework installed, we'll need to enable it to load the CSS file into our Angular app. For this, just place the relative path within the .angular-cli.json file., to the file in the styles array more precisely. ... "styles": [ "../node_modules/bulma/css/bulma.css", "styles.css" ], ...   “Tempted” to display some icons now, as well? Then go ahead and add the font-awesome library as cdn link. For this, just include the stylesheet right into your index.html: &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"&gt; Et voila! This is how you create an Angular project with Angular CLI! What do you think? Is the Angular command-line interface an extremely useful tool to jumpstart your project with or did you expected your “starter kit” to include, right out-of-the-box, more elements to get you started? ... Read more
RADU SIMILEANU / May 25'2018
How to Use Gulp To Streamline Your Theme Development Process in Drupal 8
Automate all site-building tasks and streamline your theme development process! In short: reduce development time and boost your productivity as a Drupal 8 themer with Gulp! But how to use Gulp to automate your workflow? How do you set it up and run its toolchain in conjunction with Drupal? How do you move away from the old practices of the “good old days”,  when you had to write your themes in pure CSS? Since you had no alternative... How do you “harness Gulp's power” to automate tasks that are crucial for your Drupal 8 theming process such as:   compiling SASS and LESS watching/generating CSS files from SASS testing generating static design pattern pages reloading the browser  optimizing assets: images, JavaScript, CSS   Well, keep reading and “thou shalt find out”!   But What Is Gulp, After All? I confess I couldn't come up with a better definition for Gulp, than the one on the official site: "… a toolkit for automating painful or time-consuming tasks in your development workflow, so you can stop messing around and build something." More often than not referred to as “build tools”, Gulp is a front-end automation tool aimed at streamlining your theme development process. Basically, it enables you to have all those tasks that are critical (and time-consuming) for your workflow run... automatically! And thus to give your productivity as a Drupal themer a significant boost!   Why Use Gulp over Other Theme Development Streamlining Tools? Here are 3 of the strongest reasons why:   because Gulp configurations are conventionality simpler, shorter because using Gulp you get to capitalize on a wider community support because it runs faster, as well   How to Install Gulp A word of caution: since Gulp is a node module, make sure you install Node.js (this, of course, if you haven't installed it already) first things first. Needless to add that:   you should go for the latest version of Node.js the installation process depends (and may differ) greatly on the Operation System that you're using   Install Homebrew, then run the following command: $ brew install node Now, for installing Gulp, type the following command: npm install gulp-cli -g Where “npm” stands for the “Node Package Manager” used for installing Gulp on your PC. And where the “-g” flag signals that Gulp should be installed globally on your PC.   Creating Your Gulp Project: Set Up Your Package.json File As you well know, Node.js uses “npm”, a package manager “in charge” with downloading and installing all the modules required for a given project. And all these modules get defined in a “package.json” file.  So, this is the step you, too, should take now: setting up your package.json file in the very root of your Drupal 8 installation. For this, just run the following command inside that directory: npm init It's inside this newly created package that all the key information about the node packages required for your specific project gets stored! Therefore, make sure to fill up all the needed information as you put together your package.json. Note: still, you should also know that all the prompts are ideally intuitive and that even if you leave those fields blank, you can always come back later and change the default values. Remember to set the entry point to “gulpfile.js” and, once you add the required information for your project, keep in mind to specify your git repository. An Important Word of Caution! Your “How to use Gulp to automate my theming tasks in Drupal 8?” question should be accompanied by: “How to prevent triggering a segmentation fault?” One that you might involuntarily trigger once you run Drush. Since Drush will consider the node package's own .info file files as part of Drupal and since it doesn't recognize their format... trouble happens. Here's how you avoid segmentation fault: add a script to the package.json file, whose main role will be to remove all the .info files from the node_modules folder And now, let's go back to our package.json setting up process! One last piece of advice that I'd like to add: if you've run the “npm init” command for creating your package.json file, remember to identify the “scripts” section and to replace the following line: "test": "echo \"Error: no test specified\" &amp;&amp; exit 1" … with this one: "postinstall": "find node_modules/ -name '*.info' -type f -delete" And one more thing: set up the following .npmrc file in your theme folder's root: unsafe-perm = true Here's how the “intro” of your package.jso file' structure will look like: "name": "...",   "version": "...",   "description": "...",   "main": "...",   "author": "...",   "private": true,   "devDependencies": {     "browser-sync": "...",     "gulp": "...", Once you have your package nicely set up, go ahead and install Gulp into your project by running this command: npm install gulp --save-dev  Note: you might be wondering why you need to install Gulp... again. The answer is simple: first, you installed Gulp globally and now you're installing it into your Drupal project! In this respect, it's  “--save-dev” which will let your PC know that Gulp needs to be added as a dev dependency in your packgage.json. All Gulp files and dependencies associated with it will get downloaded and a “node_modules” folder will get created.   Set Up Your Gulpfile.js You're not yet done with setting everything up! There's still one important step to take before you discover how to use Gulp to create and to automate your site building tasks: setting up the gulpfile.js! For this, just:   go to your custom theme directory manually create a gulpfile.js file by either using the cmd/terminal or just by right-clicking to create a new file   Note: keep in mind to change the config variable so that it should match your paths! Also, make sure that all your CSS gets stored in the same “CSS” directory in your theme (for example: sites/all/themes/my_theme/css/application.sass)    How to Use Gulp to Create and Automate Key Tasks for Your Workflow And finally, we've reached our “destination”!  The very goal of this blog post after all: determining which site building tasks are crucial for my/your own workflow, creating them and then automating them all with Gulp! Do you fancy automated:   Sass compiling into CSS? cache clearing “detection” of all the changes you will have applied to your .scss files? page reloading?   And your “wishlist” might go on...  Well, the answer to your “How to use Gulp to automatically run task “x” or “y”?” is simple:   Just run the gulp [task_name] “magic formula” in your terminal!    Here are some examples:    gulp.task('watch', function () which will watch for all the changes applied to your scss files and automatically recompile  gulp.task('reload', ['clearcache'], function () which will clear cache, then automatically reload the page  gulp.task('browser-sync', function() which will automatically run the BrowserSync server In order to get the whole process started, simply type “gulp” in your terminal. The END! Your turn now! Time to use Gulp to its full potential and to automate all those key (and time-consuming) aspects of your theme development process in Drupal 8!  ... Read more
Silviu Serdaru / Feb 28'2018