42

I usually generate classes with the angular 2 cli with the following command:

ng g class todo

How can I tell the cli to generate the classes in a particular folder, for example a models folder.

The documentation has an example where they generate a component in a specific folder but I've had no success with the class option

I've tried the following options:

ng g class models/todo

ng g class ../models todo

ng g class models todo

They result in either an error that the path is invalid, which makes me think that the option is supported but I can't figure it out or in the case of the latter merges the intended folder name with the class name.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
George Bora
  • 1,618
  • 6
  • 26
  • 45

8 Answers8

45

Generated items for the Angular CLI are relative...

if you are in the root dir (or the source dir) you are perceived to be in the app dir...

~/me/foo > ng g c bar
// ~/me/foo/src/app/bar/bar.component*

~/me/foo/src > ng g c bar
// ~/me/foo/src/app/bar/bar.component*

if you are in the app directory or further down the folder structure you will generate to where you are...

~/me/foo/src/app/feature > ng g c bar
// ~/me/foo/src/app/feature/bar/bar.component*

~/me/foo/src/app/feature > ng g c ../bar
// ~/me/foo/src/app/bar/bar.component*

Note: if you try to generate into a dir that does not exist, it will error...

~/me/foo/src/app > ng g c a/b/c/d/e/f/g/bar
// Error
Brocco
  • 62,737
  • 12
  • 70
  • 76
  • The component example already worked (that was in the documentation), what I'm interested is in classes, nonetheless your answer did provide the key: if I create the folder before hand I can generate the class directly to it. – George Bora Oct 26 '16 at 06:30
  • 1
    class generation works the same way , except classes don't generate their own directory by default. – Brocco Oct 26 '16 at 11:12
  • Yes that is the whole point of my question can I generate a specific directory when I generate my class, the cli can't but if I create the folder before hand I can generate the class directly in it from the cli. – George Bora Oct 26 '16 at 12:43
  • 1
    OK. I'd suggest you add your thoughts/opinions to this github issue... https://github.com/angular/angular-cli/issues/2806 – Brocco Oct 26 '16 at 15:34
  • Thanks for the link. – George Bora Oct 26 '16 at 20:48
11

To create a todo component inside the models folder in your project, just make sure you are in the project's root folder and type:

ng g c /models/todo

Also, if you want to specify a the declaring module's file name, you can use the -m option.

For example, if you have a models module, and want to update that module for declare your new todo's component:

ng g c /models/todo -m model

Regards

Ivan Maia
  • 251
  • 2
  • 6
7
 ng g cl model/UserInfo
works fine in angular5
7

For generating class in Angular 4/5/6

Just right click on the folder in which you want to create the class and select the folder open in terminal

then inside the terminal use

ng g class classname --type=model
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
ayush mishra
  • 103
  • 2
  • 8
5
ng generate component my-new-component

ng g component my-new-component # using the alias

components support relative path generation

if in the directory src/app/feature/ and you run

ng g component new-cmp

your component will be generated in src/app/feature/new-cmp

but if you were to run

ng g component ../newer-cmp

your component will be generated in src/app/newer-cmp

that works for classes also

check : Github angular-cli

Hellium
  • 7,206
  • 2
  • 19
  • 49
Mourad Idrissi
  • 3,455
  • 5
  • 17
  • 29
3

adding to Karthikeyan Karunanithi simple answer,

ng g i model/UserInfo

works great if you wanted an interface...

Robbotnik
  • 557
  • 8
  • 8
3
ng g cl models//todo-class

result ~/project/src/app/models/todo-class.ts

export class TodoClass {}
Elton Sandré
  • 372
  • 3
  • 7
2

https://github.com/angular/angular-cli#generating-components-directives-pipes-and-services

Generating Components, Classes, Directives, Guards, Interfaces, Enums, Pipes and Services, Modules, Libraries

You can use the ng generate (or just ng g) command to generate Angular artifacts:

ng generate component my-new-component
ng g component my-new-component # using the alias

components support relative path generation

if in the directory src/app/feature/ and you run

ng g component new-cmp

your component will be generated in src/app/feature/new-cmp

but if you were to run

ng g component ./newer-cmp

your component will be generated in src/app/newer-cmp

if in the directory src/app you can also run

ng g component feature/new-cmp

and your component will be generated in src/app/feature/new-cmp

You can find all possible blueprints in the table below:

Scaffold    Usage
Component   ng g component my-new-component
Directive   ng g directive my-new-directive
Pipe    ng g pipe my-new-pipe
Service ng g service my-new-service
**Class ng g class my-new-class**
Guard   ng g guard my-new-guard
Interface   ng g interface my-new-interface
Enum    ng g enum my-new-enum
Module  ng g module my-module

angular-cli will add reference to components, directives and pipes automatically in the app.module.ts. If you need to add this references to another custom module, follow this steps:

ng g module new-module to create a new module

call ng g component new-module/new-component

This should add the new component, directive or pipe reference to the new-module you've created.

Byron
  • 691
  • 9
  • 9