Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no Angular-CLI generate command for 'model' in Angular Project?

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

like image 432
Deadpool Avatar asked Oct 26 '18 07:10

Deadpool


People also ask

What is the command to create a model in Angular?

Therefore to generate angular model through angular-CLI, you need to use class schematic with the option –type=model . And there is no special schematic or sub commands available in the name of model.


3 Answers

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
like image 101
Outman Avatar answered Oct 24 '22 18:10

Outman


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.

like image 31
Nimezzz Avatar answered Oct 24 '22 19:10

Nimezzz


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
like image 2
mikolaj semeniuk Avatar answered Oct 24 '22 18:10

mikolaj semeniuk