84

While learning up Angular I just went through the various Angular-CLI commands to generate individual parts of Angular like 'Component', 'Services', 'Interface', 'Pipes', etc.

Generating Angular Items via Angular-CLI

ng g c components/comp-1 //generates component
ng g s services/service-1 // generates service
ng g i interfaces/interface-1 // generates interface

But, I am amazed why there is no generate command for 'Model' (though Interface also does nearly some work -- but model is more powerful as can contain methods also in Class).

Am I missing something or Team-Angular missed on generating a command for 'automatically generating Models' -- as they are at the very core of OOPS Framework.

enter image description here

Reference:

https://www.npmjs.com/package/angular-cli

Deadpool
  • 7,811
  • 9
  • 44
  • 88

6 Answers6

241

Because a model is a class, to generate it use --type option like this:

ng generate class hero --type=model

will result in:

hero.model.ts
Outman
  • 3,100
  • 2
  • 15
  • 26
  • 5
    But, then interface, pipes, modules are also clases. Aren't they? – Deadpool Oct 26 '18 at 07:58
  • 6
    You are correct, they technically are. But a model is the closest to a generic class by being a skeleton of data (But this isn't an excuse really). I'm amazed as well they didn't just add the `model` command albeit it isn't a reserved word or anything. – Outman Oct 26 '18 at 08:08
  • 4
    If you need to avoid creating unit test class hero.model.spec.ts simply type --spec=false. For example, ng generate class hero --type=model --spec=false – Ishwor Khanal Feb 24 '19 at 00:49
  • FYI this doesn't work with the shortened version: `ng g c hero --type=model`. You have to type everything out like in the answer. – Patrick May 15 '20 at 02:32
  • 2
    @Patrick You only have to type out the word 'class'. So in your example type: `ng g class hero --type=model` and you will get your hero.model.ts and the associated .spec file – djmarquette Jun 24 '20 at 21:55
  • Thanks. Weird that `g` can be abbreviated but not `c`. – Patrick Jun 25 '20 at 18:29
  • 3
    @Patrick that's because the abbreviation `c` is for `component` (and `s` is for `service`). The command I often use is `ng generate interface hero --type=model` or `ng generate i hero --type=model` because it creates an interface instead (and it does not create the spec/test file). – BoDeX Mar 03 '21 at 03:39
17

You cannot generate a model directly. Actually model is a class. ng generate allows only following types to generate by default.

  1. appShell
  2. application
  3. class
  4. component
  5. directive
  6. enum
  7. guard
  8. interface
  9. library
  10. module
  11. pipe
  12. service
  13. serviceWorker
  14. universal
  15. webWorker

So in your case you can use --type option to define a generate classes. Let's assume you want a class like bar.foo.ts

You just have to define it with following option.

ng generate class bar --type=foo

in your case you can define a module with this command

ng generate class nameOfYourModule --type=model

It will generate nameOfYourModule.model.ts

Please refer this official documentation of ng generate options for more informations.

Nimezzz
  • 1,814
  • 14
  • 15
3
ng g class subfolder/yourclass --type=model --skip-tests 

will create a class yourclass in app\subfolder. If you remove --skip-tests, you will get an extra file yourclass.model.spec.ts.

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
2

Just run in root of project:

ng generate interface your_model_name --type=model
# or in models folder
ng generate interface models/your_model_name --type=model
mikolaj semeniuk
  • 2,030
  • 15
  • 31
0

This command also generate model

ng generate class employee --type

Result In:

employee.ts employee.spec.ts

selva kumar
  • 147
  • 1
  • 1
  • 8
0

I just use ng generate class (without any flags), and I believe many others do as well. I think the only time I use a pure class file in Angular is with models. Since they are mostly in the models folder, there's little reason to add additional suffix of 'model' by using --type=model. I believe that's part of the reasons why the Angular team did not include the 'ng generate model' command (since it is duplicative of class).

Jason
  • 657
  • 7
  • 13