What Is Angular CLI?
The Angular Team created the command-line interface, or CLI, for Angular. With the aid of this tool, we can immediately begin building the Angular Application. Using the Angular CLI commands, we will develop and manage the Angular app in this article. The commands ng new, ng generate component, ng generate directive, & ng generate pipe will be covered, among others.
Why Angular CLI
Front End Framework Angular is simple to learn. However, creating a development environment is quite difficult. When you select Angular as your JavaScript Framework, you have a lot of options.
Dart, Typescript, and Javascript are your options. SystemJs, Webpack, and other module loaders must be selected. Pick up your testing framework immediately. All of this was covered in our article on setting up an Angular development environment.
Once a decision has been made, numerous libraries and packages have been set up. These libraries each have their own configuration files included. For instance, you must add all necessary libraries to the Package.Json file that you build. Make configuration files for Webpack or SystemJS. For Typescript, configuration files are also necessary.
What is Angular CLI
With just one command, the Angular CLI enables you to quickly create an Angular application with all the necessary configuration files and packages. It also enables us to enhance already-built Angular apps with new functionality (components, directives, services, etc.). It assists us in testing, creating, and distributing our application.
The Angular CLI leverages Typescript, Webpack (for module bundling), Karma (for unit testing), and Protractor (for end-to-end testing) to generate the Angular application.
Installing Angular CLI
Installing the Angular CLI is the first step. The following command can be used to accomplish this.
npm install -g @angular/cli@latest
Angular CLI Versions
Angular CLI Version Check
ng --version
Angular CLI Commands
Commad | Alias | Description |
---|---|---|
help | Help message shows the List of available commands and their short descriptions. | |
version | v | Find out the version of the Angular CLI Installed |
new | n | Creates a new folder (Workspace) and ads the initial Angular app. Note that you can create multiple apps in a single folder (Workspace) |
add | Adds the npm package to the workspace and configure the default app project to use that library. | |
generate | g | Generates and/or modifies files based on a schematic. |
update | Updates your application and its dependencies | |
serve | s | Builds and serves your app, rebuilding on file changes. |
run | Runs a custom target defined in your project. | |
build | b | The Compiles the Angular app into an output directory ( Default is dist) |
test | t | Runs unit tests in a project. |
e2e | e | Builds and serves an Angular app, then runs end-to-end tests using Protractor. |
config | Retrieves or sets Angular configuration values. | |
doc | d | Opens the official Angular documentation (angular.io) in a browser, and searches for a given keyword. |
lint | l | Runs linting tools on Angular app code in a given project folder. |
xi18n | Extracts i18n messages from source code. |
Getting Help
ng help
The syntax ng [command name] --help is used to request help for certain commands. As An Example
ng add --help //help on add command ng new --help //help on new command
Creating the Application with ng new
ng new
- What name do you want to give the endeavor?
The project's name should be entered here. “GettingStarted” - Do you want to include Angular Routing?
Unless you do not wish to add Angular Routing, select Yes. - Which stylesheet format are you interested in using?
You can use the arrow keys to choose from the various CSS, SCSS, SASS, LESS, and Stylus options.
- a new directory has been created The creation of GettingStarted
- installs Angular libraries and any other dependencies that need to be downloaded.
- configures and installs TypeScript.
- Karma and Protractor are installed and set up for testing.
- Build the Git from scratch.
Running the Application
cd gettingStarted ng serve // or npm start
ng new options
Options | Alias | DESCRIPTION |
---|---|---|
--dry-run | -d | Run through without making any changes. |
--force | -f | Forces overwriting of any existing files in the project folder |
--verbose | -v | Displays out of the command |
--collection | -c | Schematics to use. For more info on Schematics click here. |
--inline-style | -s | Use inline style rather than the external StyleSheet file. does not create external Stylesheets |
--inline-template | -t | Does not create an external template file for the component. Specifies if the template will be in the ts file. |
--view-encapsulation | Specifies the view encapsulation strategy. Three Options are available here Emulated , Native &. None Default is Emulated | |
--routing | Generates a routing module. If the option is not specified, it will ask for the confirmation | |
--prefix | -p | The file extension is to be used for style files. The values available are CSS , SCSS , SASS ,LESS , and Stylus . If the options are not specified, it will ask to select the appropriate style when running the command |
--skip-tests | -S | Skip creating test spec files. This option does not seem to remove the test-related files. Check the bug report here. Use the --minimal option instead |
--skip-package-json | Do not add dependencies to package.json | |
--minimal | Installs the minimal set of features. Does not create test files. Creates inline style & templates |
ng new example
//creates with new project with Style CSS ng new gettingStarted --routing --style CSS
//creates the new project with HelloWorld_root as the selector ng new HelloWorld --routing --style CSS -p HelloWorld
ng generate
ng generate <schematic> [options] ng g <schematic> [options]
Components, modules, classes, pipes, and directives can all be generated with ng generate or (ng g). The list of possible artifacts is displayed in the tables below.
schematic | Syntax | DESCRIPTION |
---|---|---|
appShell | ng g appShell [options] | Generate an App shell. Read about App Shell from here |
application | ng g application | Generates an application |
class | ng g class | Generates Class file |
component | ng g component | Generates a component |
directive | ng g directive | Generates a Directive |
enum | ng g enum | Generates an enum |
guard | ng g guard | Generates a Guard Component |
interface | ng g interface | Generates an Interface |
library | ng g library | Generates a Library |
module | ng g module | Generates a Module |
pipe | ng g pipe | Generates a Pipe |
service | ng g service | Generates a Service class |
serviceWorker | ng g serviceWorker [options] | Generates a Service worker |
universal | ng g universal [options] | Generates a Universal |
Common options
OPTION | Alias | Default | DESCRIPTION |
---|---|---|---|
--defaults=true|false | false | When true, disables interactive input prompts for options with a default | |
--dryRun=true|false | -d | false | When true, run through and report activity without writing out results. |
--force=true|false | -f | false | When true, force overwriting of existing files |
--help= | false | Shows a help message for this command in the console. | |
--interactive=true|false | false | When false, disables interactive input prompts. |
Component
ng g component <name> [options]
Examples
ng g component Hello
- src/app is modified to include the Hello folder.
- Create the HelloComponent in the Hello folder, along with the CSS, Spec, and Template files.
- the root module imports the HelloComponent and adds it to the declarations array.
- App-name> is the format used by the CSS selector. e.g., app-hello
generate components inside the module
OPTION | Alias | Default | DESCRIPTION |
---|---|---|---|
--changeDetection= Default|OnPush | -c | Default | Specifies the change detection strategy. |
--entryComponent= true|false | false | Specifies if the component is an entry component of declaring module. | |
--export=true|false | false | Specifies if declaring module exports the component. | |
--flat= true|false | false | Flag to indicate if a directory is created. | |
--inlineStyle= true|false | -s | false | Specifies if the style will be in the ts file. |
--inlineTemplate=true|false | -t | false | Specifies if the template will be in the ts file. |
--lintFix= true|false | false | Specifies whether to apply lint fixes after generating the component. | |
--module= module | -m | root module | Allows specification of the declaring module. |
--prefix= prefix | -p | The prefix to apply to generated selectors. | |
--project= project | The name of the project. | ||
--selector= selector | The selector to use for the component. | ||
--skipImport= true|false | false | Flag to skip the module import. | |
--spec= true|false | true | Specifies if a spec file is generated. | |
--styleext= styleext | css | The file extension to be used for style files | |
--viewEncapsulation= | -v | Emulated | Specifies the view encapsulation strategy. |
Directive
import { Directive } from '@angular/core'; @Directive({ selector: '[appSome]' }) export class SomeDirective { constructor() { } }
OPTION | Alias | Default | DESCRIPTION |
---|---|---|---|
--export=true|false | false | Specifies if declaring module exports the component. | |
--flat= true|false | true | Flag to indicate if a directory is created. | |
--lintFix= true|false | false | Specifies whether to apply lint fixes after generating the component. | |
--module= module | -m | root module | Allows specification of the declaring module. |
--prefix= prefix | -p | The prefix to apply to generated selectors. | |
--project= project | The name of the project. | ||
--selector= selector | The selector to use for the component. | ||
--skipImport= true|false | false | Flag to skip the module import. | |
--spec= true|false | true | Specifies if a spec file is generated. |
Pipe
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'date' }) export class DatePipe implements PipeTransform { transform(value: any, args?: any): any { return null; } }
OPTION | Alias | Default | DESCRIPTION |
---|---|---|---|
--export=true|false | true | Specifies if declaring module exports the component. | |
--flat= true|false | true | Flag to indicate if a directory is created. | |
--lintFix= true|false | false | Specifies whether to apply lint fixes after generating the component. | |
--module= module | -m | root module | Allows specification of the declaring module. |
--project= project | The name of the project. | ||
--skipImport= true|false | false | Flag to skip the module import. | |
--spec= true|false | true | Specifies if a spec file is generated. |
Service
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class DataService { constructor() { } }
OPTION | Alias | Default | DESCRIPTION |
---|---|---|---|
--flat= true|false | true | Flag to indicate if a directory is created. | |
--lintFix= true|false | false | Specifies whether to apply lint fixes after generating the component. | |
--project= project | The name of the project. | ||
--spec= true|false | true | Specifies if a spec file is generated. |
class
export class Customer { }
To create the class in the class folder, type the command ng g class/customer class.
OPTION | Alias | Default | DESCRIPTION |
---|---|---|---|
--project= project | The name of the project. | ||
--spec= true|false | true | Specifies if a spec file is generated. | |
--type=type | default | Specifies the type of class |
Module
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; @NgModule({ declarations: [], imports: [ CommonModule ] }) export class AccountModule { }
Module addition of components, pipes, and directives
ng g module account ng g component --module=account account/hello ng g directive --module=account --flat=false account/Some ng g service --module=account --flat=false account/Data ng g pipe --module=account --flat=false account/Date
The complete list of choices is provided below.
OPTION | Alias | Default | DESCRIPTION |
---|---|---|---|
--flat= true|false | false | Flag to indicate if a directory is created. | |
--module=module | -m | Allows specification of the declaring module. | |
--project= project | The name of the project. | ||
--routing=true|false | false | Generates a routing module. | |
--routingScope=Child|Root | child | The scope for the generated routing. |