ng generate directive command in Angular CLI
ng generate directive
command is used to create directives in Angular applications
ng generate directive
syntax
ng generate directive
takes directive name as parameter.
Additionally we can pass different options as well.
ng generate directive [name] [options]
or you can use shorthand syntax
ng generate d [name] [options]
ng generate directive
arguments
ng generate directive
takes one argument i.e, name
, nothing but directive name.
The name
type is string.
ng generate directive
options
ng generate directive
accepts 9 different types of options.
- export
- flat
- module
- prefix
- project
- selector
- skip-import
- skip-tests
- standalone
Option | Description | Type | Default Value |
---|---|---|---|
export | The declaring NgModule exports this directive. | boolean | false |
flat | When true (the default), creates the new files at the top level of the current project. | boolean | true |
module | The declaring NgModule. Aliases: -m | string | |
prefix | A prefix to apply to generated selectors. Aliases: -p | string | |
project | The name of the project. | string | |
selector | The HTML selector to use for this directive. | string | |
skip-import | Do not import this directive into the owning NgModule. | boolean | false |
skip-tests | Do not create "spec.ts" test files for the new class. | boolean | false |
standalone | Whether the generated directive is standalone. | boolean | false |
ng generate directive
example
For example if you want to create a directive named product, use ng generate directive product
command.
CREATE src/app/product.directive.spec.ts (228 bytes)
CREATE src/app/product.directive.ts (143 bytes)
UPDATE src/app/app.module.ts (471 bytes)