
- Angular CLI Tutorial
- Angular CLI - Home
- Angular CLI - Overview
- Angular CLI - Environment Setup
- Angular CLI Commands
- Angular CLI - ng version
- Angular CLI - ng new
- Angular CLI - ng help
- Angular CLI - ng generate
- Angular CLI - ng build
- Angular CLI - ng run
- Angular CLI - ng serve
- Angular CLI - ng lint
- Angular CLI - ng test
- Angular CLI - ng e2e
- Angular CLI - ng add
- Angular CLI - ng analytics
- Angular CLI - ng config
- Angular CLI - ng doc
- Angular CLI - ng update
- Angular CLI - ng xi18n
- Angular CLI - Code Coverage
- Angular CLI - ng cache Command
- Angular CLI - ng completion Command
- Angular CLI - ng deploy Command
- Angular CLI - ng extracti18n Command
- Angular CLI Useful Resources
- Angular CLI - Quick Guide
- Angular CLI - Useful Resources
- Angular CLI - Discussion
Angular CLI - ng generate Command
The ng generate is a built-in command of Angular CLI. It is used to generate and/or modifies files related to various Angular entities like components, directives, services, modules, pipes, interfaces and more.
If you want, you can manually create those classes and files without using the ng generate command in three simple steps. Create the required files yourself, write the code for each file, and update the relevant module or other configurations. However, it will consume more time.
Syntax
The syntax of ng generate command is as follows −
ng generate[options] Or, ng g [options]
Argument
The argument for ng generate command is as follows −
Sr.No. | Argument & Syntax | Description |
---|---|---|
1 | The schematic or collection:schematic to generate. This option can take one of the following sub-commands:
|
Options
The table below contains all the optional parameters of ng generate command −
Sr.No. | Option & Syntax | Description |
---|---|---|
1 | --defaults=true|false | By default its value is "false". When this option is "true", it disables interactive input prompts for options with a default. |
2 | --dryRun=true|false |
When "true", runs through and shows action to be taken without applying changes to the files or the project. Default: false. Aliases: -d. |
3 | --force=true|false |
When "true", forces overwriting of existing files. Default: false. Aliases: -f. |
4 | --help=true|false|json|JSON |
If you need any help with this command, use --help along with the command in console. Default: false. |
5 | --interactive=true|false | When "false", disables interactive input prompts. By default, it is "true". |
Example 1
Let's see how we can create a component in Angular application using the ng generate command −
> ng generate component goals CREATE src/app/goals/goals.component.html (20 bytes) CREATE src/app/goals/goals.component.spec.ts (621 bytes) CREATE src/app/goals/goals.component.ts (271 bytes) CREATE src/app/goals/goals.component.css (0 bytes)
Example 2
In this example, we are going to create a Service using ng generate command:
> ng generate service print-name CREATE src/app/print-name.service.spec.ts (389 bytes) CREATE src/app/print-name.service.ts (147 bytes)
Similarly, you can use ng generate command to generate other Angular entities.