Why no Angular-CLI generate command for 'model' in Angular Project?
JavascriptAngularAngular CliJavascript Problem Overview
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.
Reference:
Javascript Solutions
Solution 1 - Javascript
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
Solution 2 - Javascript
You cannot generate a model directly. Actually model is a class. ng generate allows only following types to generate by default.
- appShell
- application
- class
- component
- directive
- enum
- guard
- interface
- library
- module
- pipe
- service
- serviceWorker
- universal
- 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.
Solution 3 - Javascript
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
Solution 4 - Javascript
This command also generate model
ng generate class employee --type
Result In:
employee.ts
employee.spec.ts
Solution 5 - Javascript
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).
Solution 6 - Javascript
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
.