Friday, 28 December 2018

How to create a directive directives in Angular ?

To create a directive:

ng generate directive directiveName

Or

ng g d directiveName

If your angular application contains multiple modules you will get an error because Angular CLI does not know with which module the newly generated directive should be registered.

To avoid this you can use 

ng g d directiveName --skip-import

Angular CLI don't import and register the generated directive with any module.

Or you can use below command to tell the angular cli with which module it should register when multiple modules are present in your application.

ng g d directiveName -m=app.module

In the above, it will register to the app module in your angular application

You can use --flat option to create a dedicated folder for the directive.
You can use --spec to specifies if a spec file should be generated or not by passing true or false value.

No comments:

Post a Comment