Angular Cli
Angular Cli
#angular-cli
Table of Contents
About 1
Remarks 2
Examples 2
Installation or Setup 2
Introduction 4
Examples 4
Neccessary steps taken before deploying the angular-cli project for production build. 4
Creating the war file for production deployment of angular-cli project on apache tomcat se 4
Introduction 6
Syntax 6
Parameters 6
Examples 7
Generating components 8
Generating directives 9
Generating services 10
Generating pipes 11
Generating modules 12
Credits 14
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: angular-cli
It is an unofficial and free angular-cli ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official angular-cli.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ 1
Chapter 1: Getting started with angular-cli
Remarks
This section provides an overview of what angular-cli is, and why a developer might want to use it.
It should also mention any large subjects within angular-cli, and link out to the related topics. Since
the Documentation for angular-cli is new, you may need to create initial versions of those related
topics.
Examples
Installation or Setup
Note: Angular CLI versions are under rapid development. This documentation targets for the latest
version.
Prerequisites
To execute and work with the latest version, Angular CLI and the project have dependencies that
require node v6.9.0 or higher.
Setup
Make sure that a node version is installed which is compatible with the CLI
npm i @angular/cli -g or yarn global add @angular/cli, depending on the package manager in use.
To create a new project ng new [project-name] which initializes git.Install packages for tooling via
npm. It creates the project successfully.
Application will refresh according to the changes made in the directory. The default HTTP port and
the one used by the LiveReload server can be changed using below command
https://riptutorial.com/ 2
ng serve --host 0.0.0.0 --port 4201 --live-reload-port 49153
https://riptutorial.com/ 3
Chapter 2: angular-cli project deployment on
apache tomcat 8.0.14 server
Introduction
This topic would cover how angular-cli project is ready for production build, what all necessary
steps taken before deploying, how to create war file for project deployment and finally how to
configure the apache tomcat for angular-cli project deployment.
Examples
Neccessary steps taken before deploying the angular-cli project for
production build.
Before deploying the angular project in server we need to build angular project for production. We
also need to change the routing path in index.html file from <base href=”/”> to <base href=”./”> if it
is not done then your project wouldn’t get loaded properly there will be some routing error saying
404 file not found.
ng build –prod
Above given command with extra option like –prod would generate the production build project
bundle. Once the above command gets executed in the root directory of your project would appear
a directory called dist. In which all the production build bundle of your project appears in it.
Once the dist directory is ready with your production built bundles. Just open the dist directory
and open the command prompt type the following command to create the war file to deploy your
project on apache tomcat server.
https://riptutorial.com/ 4
jar cvf dist.war .
Once the above jar commands gets executed. It would generate a dist.war file within the dist
directory.
1. Cut/Copy the dist.war file from dist directory and place it in apache tomcat webapp
directory.
2. Go to apache tomcat bin folder and double click on startup.bat file.
3. Now tomcat server will execute dist.war file and startup the tomcat catalina server.
4. Once the tomcat catalina server gets started open web browser and type the
localhost:8080/dist and tap on enter key your project gets executed on the web browser
window.
https://riptutorial.com/ 5
Chapter 3: Working with angular-cli:
Generating components, directives, pipes,
services, etc.
Introduction
The angular-cli tool can help you to scaffold different parts of an angular application (components,
directives, pipes, services, classes, guards, interfaces, enums and modules).
Syntax
• ng generate [component | directive | service | pipe | class | enum | interface | guard | module]
[name] [flags...]
• ng g [c | d | s | p | cl | e | i | g | m] [name] [flags...]
Parameters
Parameter Description
https://riptutorial.com/ 6
Parameter Description
Examples
"Generate command" usage
You can use the ng generate or ng g command to generate Angular building blocks (components,
services, pipes, etc.).
So, for example, if you run ng generate component user-list - angular-cli will:
• create user-list directory in src/app folder or folder where you have run the command.
• inside that directory generate 4 files (user-list.component.ts, user-list.component.html, user-
https://riptutorial.com/ 7
list.component.cssand user-list.component.spec.ts)
• add user-list as a declaration in the @NgModule decorator of the nearest module.
Generating components
$ ng g c user-list
installing component
create src/app/user-list/user-list.component.css
create src/app/user-list/user-list.component.html
create src/app/user-list/user-list.component.spec.ts
create src/app/user-list/user-list.component.ts
update src/app/app.module.ts
prefix prevents element name collisions with components in other apps and with native
HTML elements. So, for example, if prefix is app - generated component will have app-
user-list selector.
@Component({
selector: 'user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.css']
})
export class UserListComponent {}
• To prevent .spec files creation add --spec false or -sp false flag
installing component
create src/app/user-list/user-list.component.css
create src/app/user-list/user-list.component.html
create src/app/user-list/user-list.component.ts
update src/app/app.module.ts
• To use inline html templates instead of external templates add --inline-template or -it flag
$ ng g c user-list --inline-template
installing component
create src/app/user-list/user-list.component.css
create src/app/user-list/user-list.component.spec.ts
create src/app/user-list/user-list.component.ts
update src/app/app.module.ts
https://riptutorial.com/ 8
• To use inline styles instead of external styles add --inline-style or -is flag
$ ng g c user-list --inline-style
installing component
create src/app/user-list/user-list.component.html
create src/app/user-list/user-list.component.spec.ts
create src/app/user-list/user-list.component.ts
update src/app/app.module.ts
$ ng g c user-list --flat
installing component
create src/app/user-list.component.css
create src/app/user-list.component.html
create src/app/user-list.component.spec.ts
create src/app/user-list.component.ts
update src/app/app.module.ts
You can also combine flags listed above. For example, to create only .component.ts file without
.css, .html, .spec files and folder use the following command.
installing component
create src/app/user-list.component.ts
update src/app/app.module.ts
Generating directives
$ ng g d highlight
installing directive
create src/app/highlight.directive.spec.ts
https://riptutorial.com/ 9
create src/app/highlight.directive.ts
update src/app/app.module.ts
@Directive({
selector: '[highlight]'
})
export class HighlightDirective {}
• To prevent .spec files creation add --spec false or -sp false flag
installing directive
create src/app/highlight.directive.ts
update src/app/app.module.ts
installing directive
create src/app/highlight/highlight.directive.spec.ts
create src/app/highlight/highlight.directive.ts
update src/app/app.module.ts
You can also combine flags listed above. For example, to create only highlight.directive.ts file
inside highlight folder without .spec file use the following command.
installing directive
create src/app/highlight/highlight.directive.ts
update src/app/app.module.ts
Generating services
https://riptutorial.com/ 10
To add a service with a name UserService, run:
$ ng g s user
installing service
create src/app/user.service.spec.ts
create src/app/user.service.ts
• To prevent .spec files creation add --spec false or -sp false flag
installing service
create src/app/user.service.ts
installing service
create src/app/user/user.service.spec.ts
create src/app/user/user.service.ts
You can also combine flags listed above. For example, to create only user.service.ts file inside
user folder without .spec file use the following command.
installing service
create src/app/user/user.service.ts
Generating pipes
$ ng g p search-by-name
installing pipe
create src/app/search-by-name.pipe.spec.ts
create src/app/search-by-name.pipe.ts
update src/app/app.module.ts
• To prevent .spec files creation add --spec false or -sp false flag
https://riptutorial.com/ 11
$ ng g p search-by-name --spec false
installing pipe
create src/app/search-by-name.pipe.ts
update src/app/app.module.ts
installing pipe
create src/app/search-by-name/search-by-name.pipe.spec.ts
create src/app/search-by-name/search-by-name.pipe.ts
update src/app/app.module.ts
You can also combine flags listed above. For example, to create only search-by-name.pipe.ts file
inside folder search-by-name folder without .spec file use the following command.
installing pipe
create src/app/search-by-name/search-by-name.pipe.ts
update src/app/app.module.ts
Generating modules
$ ng g m guest
installing module
create src/app/guest/guest.module.ts
$ ng g m guest --spec
installing module
create src/app/guest/guest.module.spec.ts
create src/app/guest/guest.module.ts
https://riptutorial.com/ 12
$ ng g m guest --routing
installing module
create src/app/guest/guest-routing.module.ts
create src/app/guest/guest.module.ts
You can also combine flags listed above. For example, to create module with routing and specs
use the following command.
$ ng g m guest -sp -r
installing module
create src/app/guest/guest-routing.module.ts
create src/app/guest/guest.module.spec.ts
create src/app/guest/guest.module.ts
Read Working with angular-cli: Generating components, directives, pipes, services, etc. online:
https://riptutorial.com/angular-cli/topic/9482/working-with-angular-cli--generating-components--
directives--pipes--services--etc-
https://riptutorial.com/ 13
Credits
S.
Chapters Contributors
No
angular-cli project
deployment on
2 Gurudath G
apache tomcat
8.0.14 server
Working with
angular-cli:
Generating
3 Ketan Akbari, Saka7
components,
directives, pipes,
services, etc.
https://riptutorial.com/ 14