This article will demonstrate how to create an Angular 5 project using CLI and implement ngx-bootstrap or Bootstrap 3 with it. Here we are not going to explain what Angular 5 is or the difference between Angular 2 and Angular 5. We believe you have basic knowledge about Angular and Angular 5. So, let's start this demonstration.
Prerequisites
Before proceeding to create a new Angular 5 project using CLI in Visual Studio Code, we must first confirm that we have a configured system which should have the prerequisites required for Angular 5 , including an editor like Visual Studio Code, Node, NPM and CLI. First, we must check the installed version of Node, Npm and CLI.
As for this demonstration, we are using Visual Studio Code editor, which is a very lightweight tool as compared to Visual Studio 2015/2017 or any other version. If you are working on client side technologies Angular, Node, React, Html, CSS etc. then it is recommended to work with Visual Studio Code.
If you don’t have Visual Studio Code installed in your system then you can download and install it from its official site: https://code.visualstudio.com/download.
After installing Visual Studio Code, open it and press CTRL+` to open terminal windows. For this demonstration, we are using Visual Studio Code, that is why we are showing how to check the version of installed components in Visual Studio Code. You can also use Command Prompt for that as well.
Once you open terminal windows in Visual Studio Code, we can use the following command to check node version. If you don’t have Node installed then you can download it from https://nodejs.org/en/ and install it and then you can check the version of Node and NPM. To check node version.
node -v //output v9.5.0
Check npm version using the following command.
npm -v //output 5.6.0
Check Angular CLI version using the following command
ng -v
If you don't have Angular CLI:
If you don’t have Angular CLI, you can install it using the following command. You should always install latest version of angular CLI. If you are interested to know more about Angular CLI, just visit https://github.com/angular/angular-cli.
npm install -g @angular/cli@latest
Note
Both the CLI and generated project have dependencies that require Node 6.9.0 or higher, together with NPM 3 or higher.
Create Angular 5 Project
To create a new Angular 5 project, just select that location in the terminal window where you want to create your Angular 5 app. And then just type the following command to create a new Angular 5 project.
ng new Angular5App //New project without Routing
OR
ng new Angular5App –routing //New project with Routing
It will take a few minutes to create an Angular 5 app. Following you can see, how and what type of files were created by CLI while creating new Angular 5 project.
- C:\Users\MKUMAR3\Desktop\angular5>ng new Angular5App --routing
- create Angular5App/e2e/app.e2e-spec.ts (294 bytes)
- create Angular5App/e2e/app.po.ts (208 bytes)
- create Angular5App/e2e/tsconfig.e2e.json (235 bytes)
- create Angular5App/karma.conf.js (923 bytes)
- create Angular5App/package.json (1297 bytes)
- create Angular5App/protractor.conf.js (722 bytes)
- create Angular5App/README.md (1027 bytes)
- create Angular5App/tsconfig.json (363 bytes)
- create Angular5App/tslint.json (3012 bytes)
- create Angular5App/.angular-cli.json (1247 bytes)
- create Angular5App/.editorconfig (245 bytes)
- create Angular5App/.gitignore (544 bytes)
- create Angular5App/src/assets/.gitkeep (0 bytes)
- create Angular5App/src/environments/environment.prod.ts (51 bytes)
- create Angular5App/src/environments/environment.ts (387 bytes)
- create Angular5App/src/favicon.ico (5430 bytes)
- create Angular5App/src/index.html (298 bytes)
- create Angular5App/src/main.ts (370 bytes)
- create Angular5App/src/polyfills.ts (3114 bytes)
- create Angular5App/src/styles.css (80 bytes)
- create Angular5App/src/test.ts (642 bytes)
- create Angular5App/src/tsconfig.app.json (211 bytes)
- create Angular5App/src/tsconfig.spec.json (283 bytes)
- create Angular5App/src/typings.d.ts (104 bytes)
- create Angular5App/src/app/app-routing.module.ts (245 bytes)
- create Angular5App/src/app/app.module.ts (395 bytes)
- create Angular5App/src/app/app.component.html (1173 bytes)
- create Angular5App/src/app/app.component.spec.ts (1103 bytes)
- create Angular5App/src/app/app.component.ts (207 bytes)
- create Angular5App/src/app/app.component.css (0 bytes)
- npm WARN deprecated [email protected]: All versions below 4.0.1 of Nodemailer are deprecated. See https:
- npm WARN deprecated [email protected]: Use uuid module instead
-
- > [email protected] install C:\Users\MKUMAR3\Desktop\angular5\Angular5App\node_modules\uws
- > node-gyp rebuild > build_log.txt 2>&1 || exit 0
-
-
- > [email protected] install C:\Users\MKUMAR3\Desktop\angular5\Angular5App\node_modules\node-sass
- > node scripts/install.js
-
- Cached binary found at C:\Users\MKUMAR3\AppData\Roaming\npm-cache\node-sass\4.8.3\win32-x64-59_binding.node
-
- > [email protected] postinstall C:\Users\MKUMAR3\Desktop\angular5\Angular5App\node_modules\webpack\node_modules\uglifyjs-webpack-plugin
- > node lib/post_install.js
-
-
- > [email protected] postinstall C:\Users\MKUMAR3\Desktop\angular5\Angular5App\node_modules\node-sass
- > node scripts/build.js
Binary found at C:\Users\MKUMAR3\Desktop\angular5\Angular5App\node_modules\node-sass\vendor\win32-x64-59\binding.node,
- Testing binary
- Binary is fine
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
added 1269 packages in 109.86s
Project 'Angular5App' was successfully created.
So, as you can see we have created an Angular 5 project successfully and added all required node packages inside the node_modules folder.
Now it's time to run this project. But before running the project, first, you should go inside the project folder in the terminal window and then you will be able to run the project. So run Angular 5 CLI project, we can use following commands:
ng serve OR ng serve –open OR ng serve -o
NOTE
If you are not inside the project folder before running the above command, you will get the following error.
node_modules appear empty, you may need to run `npm install`
Project Structure
Here we are describing important folders and files as follows.
- src
This folder contains app folders which contain all the files and folders required to create an angular app including your components, HTML page, module, page specific CSS, data files [interfaces or classes] etc.
- assets
This folder exists inside the app folder and contains the only static file like images, CSS etc.
- e2e
This folder contains required files for testing your angular app.
- node_modules
All installed packages using "npm install" command goes inside this folder.
- angular-cli.json
Angular CLI loads its configuration from angular-cli.json file to build, run and deploy the angular app.
- package.json
This file keeps information about all packages on which the Angular app has dependencies. Apart from that it also contains dev dependencies.
- tsconfig.json
This is the main configuration file for typescript. The angular compiler looks for it when you make any change in the code and saves things like the base URL, target version of javascript, whether to compile or not on save, etc.
Why ngx-bootstrap
Just to confirm here: ngx-bootstrap contains all core (and not only) Bootstrap components powered by Angular. So you don't need to include original JS components, but we are using markup and CSS provided by Bootstrap.
Reference
https://github.com/valor-software/ngx-bootstrap
Install bootstrap and ngx-bootstrap
Now we will show how to add bootstrap with this project and use a few features like Accordion, Dropdown, Tabs Modal Popup etc. to see if it is working or not. First, open your project in Visual Studio Code. To open created project into Visual Studio Code, go to File menu and select Open Folder. From the “Open Folder” windows, just select your project and click to “select folder”. Now you can see the project in Visual Studio Code in Explorer window.
There are different ways to implement bootstrap with the Angular project. We can use regular CDN files for bootstrap on the index.html page or we can install bootstrap components using npm and use ngx-bootstrap or ng-bootstrap.
- Using direct CDN path for bootstrap
- Install bootstrap using NPM
So, let's start installing two components as follows,
- npm install bootstrap@3 –save
- npm install ngx-bootstrap --save
To know more about ngx-bootstrap, you can visit its official site https://ngx-bootstrap-latest.surge.sh/#/getting-started. Here you can get an example of each component which is part of the ngx-bootstrap.
So, let's move to the demonstration part again and see how to use ngx-bootstrap components. Here we will not implement all components but give you ideas about how to implement ngx-bootstrap components with Angular 5 projects.
So, first open style.css in the parent directory and import bootstrap.min.css into it.
@import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
So, let's create a new shared module which will import all ngx-bootstrap components module and share it with the main module app.module.ts. As per separation of concern, we should always create separate module or component for single responsibility. Create a new folder inside the /src/app/ and name it “shared” and inside that create a typescript file with the name “shared-bootstrap.module.ts” and the following code.
shared-bootstrap.module.ts
- import { NgModule } from '@angular/core';
- import { AccordionModule } from 'ngx-bootstrap/accordion'
- import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
- import { ModalModule } from 'ngx-bootstrap/modal';
- import { TabsModule } from 'ngx-bootstrap/tabs';
-
- @NgModule({
- imports: [AccordionModule.forRoot(), BsDropdownModule.forRoot(), ModalModule.forRoot(), TabsModule.forRoot()],
- exports: [AccordionModule, BsDropdownModule, ModalModule, TabsModule],
- declarations: [],
- providers: []
- })
-
- export class SharedBootstrapModule {
-
- }
Here you can see, we are importing AccordionModule, BsDowndownModule, ModalModule, TabsModule from ngx-bootstrap. You can also add more modules from ngx-bootstrap.
app.module.ts
- import { BrowserModule } from '@angular/platform-browser';
- import { CommonModule } from '@angular/common';
- import { NgModule } from '@angular/core';
- import { FormsModule } from '@angular/forms';
-
- import { AppRoutingModule } from './app-routing.module';
-
- import { AppComponent } from './app.component';
- import { RouterModule } from '@angular/router/src/router_module';
- import { SharedBootstrapModule } from './shared/shared-bootstrap.module';
-
-
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- CommonModule,
- FormsModule,
- AppRoutingModule,
- SharedBootstrapModule
- ],
- exports: [],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Now we have to import SharedBootstrapModule in AppModule.
app.component.ts
- import { Component } from '@angular/core';
-
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- title = 'app';
- }
Now we have to add DOM element to create bootstrap components. As you can see with the below code, we are using bootstrap navbar along with <accordion> tag to create Accordion, <tabset> tag to create Tabs etc.
app.component.html
- <nav class="navbar navbar-inverse">
- <div class="container-fluid">
- <div class="navbar-header">
- <a class="navbar-brand" href="#">Angular 5 Example</a>
- </div>
- <ul class="nav navbar-nav">
- <li class="active">
- <a href="#">Home</a>
- </li>
- <li>
- <a href="#">Page 1</a>
- </li>
- <li>
- <a href="#">Page 2</a>
- </li>
- <li>
- <a href="#">Page 3</a>
- </li>
- </ul>
- </div>
- </nav>
-
- <div class="container">
- <div class="col-md-6">
- <h2>Accordion Example</h2>
- <accordion>
- <accordion-group heading="Static Header, initially expanded">
- This content is straight in the template.
- </accordion-group>
- <accordion-group heading="Another group">
- <p>Some content</p>
- </accordion-group>
- <accordion-group heading="Another group">
- <p>Some content</p>
- </accordion-group>
- </accordion>
-
- <br>
- <h2>Tabs Example</h2>
- <div>
- <tabset>
- <tab heading="Tab 1" id="tab1">Basic content for tab 1</tab>
- <tab heading="Tab 2">Basic content for tab 2</tab>
- <tab heading="Tab 3">Basic content for tab 3</tab>
- </tabset>
- </div>
- </div>
- <div class="col-md-6">
- <h2>Dropdown Example</h2>
- <div class="btn-group" dropdown>
- <button dropdownToggle type="button" class="btn btn-primary dropdown-toggle">
- Button dropdown
- <span class="caret"></span>
- </button>
- <ul *dropdownMenu class="dropdown-menu" role="menu">
- <li role="menuitem">
- <a class="dropdown-item" href="#">Action</a>
- </li>
- <li role="menuitem">
- <a class="dropdown-item" href="#">Another action</a>
- </li>
- <li role="menuitem">
- <a class="dropdown-item" href="#">Something else here</a>
- </li>
- <li class="divider dropdown-divider"></li>
- <li role="menuitem">
- <a class="dropdown-item" href="#">Separated link</a>
- </li>
- </ul>
- </div>
-
- <br>
- <h2>Modal Popup Example</h2>
- <button type="button" class="btn btn-primary" (click)="staticModal.show()">Open Modal Popup</button>
-
- <div class="modal fade" bsModal #staticModal="bs-modal" [config]="{backdrop: 'static'}" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"
- aria-hidden="true">
- <div class="modal-dialog modal-sm">
- <div class="modal-content">
- <div class="modal-header">
- <h4 class="modal-title pull-left">Modal Popup</h4>
- <button type="button" class="close pull-right" aria-label="Close" (click)="staticModal.hide()">
- <span aria-hidden="true">×</span>
- </button>
- </div>
- <div class="modal-body">
- This is static modal popup, backdrop click will not close it. Click
- <b>×</b> to close modal.
- </div>
- </div>
- </div>
- </div>
- </div>
-
- </div>
Now it's time to run the project. To run project just use "ng serve" command in terminal window and use "localhost:4300" in the browser and you will see the following output. Here you can see as usual Navbar, Accordion, Dropdown, Modal Popup and Tabs controls are rendering.
Click to Open Modal Popup button to open Modal popup.
Important Commands
There are several general commands that are generally used to create different types of Angular files like module, components, classes etc. We have listed a few of them as follows with shorthand commands with descriptions.
-
- ng generate module test-module
- OR
- ng g m test-module
-
-
- ng generate component test-component
- OR
- ng g c test-component
-
-
- ng generate class test-class
- OR
- ng g cl test-class
-
-
- ng generate directive test-directive
- OR
- ng g d test-directive
-
-
- ng generate service test-service
- OR
- ng g s test-service
-
-
- ng generate pipe test-pipe
- OR
- ng g p test-pipe
Conclusion
So, today we have seen how to create Angular 5 applications using CLI and implement ngx-bootstrap.
I hope this post will help you. Please provide your feedback in the comments which helps me to improve myself for next post. If you have any doubts please ask in the comment section and If you like this post, please share it with your friends. Thanks.