Working with angular-cli: Generating components, directives, pipes, services, etc.

Other topics

"Generate command" usage

You can use the ng generate or ng g command to generate Angular building blocks (components, services, pipes, etc.).

You can find all possible blueprints in the table below:

ScaffoldUsageShortened
Componentng generate component component-nameng g c component-name
Directiveng generate directive directive-nameng g d directive-name
Pipeng generate pipe pipe-nameng g p pipe-name
Serviceng generate service service-nameng g s service-name
Classng generate class class-nameng g cl class-name
Guardng generate guard guard-nameng g g guard-name
Interfaceng generate interface interface-nameng g i interface-name
Enumng generate enum enum-nameng g e enum-name
Moduleng generate module module-nameng g m module-name

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-list.component.css and user-list.component.spec.ts)
  • add user-list as a declaration in the @NgModule decorator of the nearest module.

Generating components

To add a component with a selector [prefix]-user-list, run:

  $ 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.

  • To prevent prefix usage add --prefix false or -p false flag
  $ ng g c user-list --prefix false
import { Component } from '@angular/core';

@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
  $ ng g c user-list --spec false

  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
  • 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
  • To prevent folder creation add --flat or -f flag
  $ 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.

  $ ng g c user-list -f -it -is -sp false
  
  installing component
    create src/app/user-list.component.ts
    update src/app/app.module.ts  

All generate component flags:

DescriptionFlagShortenedDefault Value
Prevent folder creation--flat-ffalse
Prevent prefix usage--prefix false-p falsetrue
Prevent .spec files creation--spec false-sp falsetrue
Enable inline html templates--inline-template-itfalse
Enable inline styles--inline-style-isfalse

Generating directives

To add a directive with a selector [prefix]Highlight, run:

  $ ng g d highlight

  installing directive
    create src/app/highlight.directive.spec.ts
    create src/app/highlight.directive.ts
    update src/app/app.module.ts
  • To prevent prefix usage add --prefix false or -p false flag
  $ ng g d highlight --prefix false
import { Directive } from '@angular/core';

@Directive({
  selector: '[highlight]'
})
export class HighlightDirective {}
  • To prevent .spec files creation add --spec false or -sp false flag
  $ ng g d highlight --spec false

  installing directive
    create src/app/highlight.directive.ts
    update src/app/app.module.ts
  • To enable folder creation add --flat false or -f false flag
  $ ng g d highlight --flat false
  
  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.

  $ ng g d highlight -f false -sp false

  installing directive
    create src/app/highlight/highlight.directive.ts
    update src/app/app.module.ts

All generate directive flags:

DescriptionFlagShortenedDefault Value
Enable folder creation--flat false-f falsetrue
Prevent prefix usage--prefix false-p falsetrue
Prevent .spec files creation--spec false-sp falsetrue

Generating services

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
  $ ng g s user --spec false
  
  installing service
    create src/app/user.service.ts
  • To enable folder creation add --flat false or -f false flag
  $ ng g s user --flat false

  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.

  $ ng g s user -f false -sp false

  installing service
    create src/app/user/user.service.ts

All generate service flags:

DescriptionFlagShortenedDefault Value
Enable folder creation--flat false-f falsetrue
Prevent .spec files creation--spec false-sp falsetrue

Generating pipes

To add a pipe with a name searchByName, run:

  $ 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
  $ ng g p search-by-name --spec false
  
  installing pipe
    create src/app/search-by-name.pipe.ts
    update src/app/app.module.ts
  • To enable folder creation add --flat false or -f false flag
  $ ng g p search-by-name --flat false
  
  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.

  $ ng g p search-by-name -f false -sp false

  installing pipe
    create src/app/search-by-name/search-by-name.pipe.ts
    update src/app/app.module.ts

All generate pipe flags:

DescriptionFlagShortenedDefault Value
Enable folder creation--flat false-f falsetrue
Prevent .spec files creation--spec false-sp falsetrue

Generating modules

To add a module called GuestModule, run:

  $ ng g m guest

  installing module
    create src/app/guest/guest.module.ts
  • To enable .spec files creation add --spec or -sp flag
  $ ng g m guest --spec

  installing module
    create src/app/guest/guest.module.spec.ts
    create src/app/guest/guest.module.ts
  • To enable routing add --routing or -r flag
  $ 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

All generate module flags:

DescriptionFlagShortenedDefault Value
Enable .spec files creation--spec-spfalse
Enable routing--routing-rfalse

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:

ParameterDescription
component or cUsed to generate component
directive or dUsed to generate directives
service or sUsed to generate services
pipe or pUsed to generate pipes
class or clUsed to generate classes
enum or eUsed to generate enums
interfaces or iUsed to generate interfaces
guard or gUsed to generate guards
module or mUsed to generate modules
--flat or -fUsed to enable/disable directory creation
--inline-template or -itUsed to enable/disable inline html templates in components
--inline-style or -isUsed to enable/disable inline styles in components
--prefix or -pUsed to disable or change prefix
--spec or -sUsed to enable/disable .spec files creation
--skip-importUsed to skip the module import
--app or -aUsed to specify app name to use
--module or -mUsed to specify the declaring module
--view-encapsulation or -veUsed to specify the view encapsulation strategy in components
--change-detection or -cdUsed to specify the change detection strategy in components
--routing or -rUsed to specify if routing module file should be generated

Contributors

Topic Id: 9482

Example Ids: 29346,29973,29977,29982,29983,29984

This site is not affiliated with any of the contributors.