angular tutorials

Angular CLI command to generate model in Angular 9

This tutorial guides you on which angular CLI command to generate model in Angular 9 application. There are various angular-CLI commands to generate various schematic or collection like components, modules, classes, pipes etc., . Let’s see what option you have in angular CLI command to generate model.

Angular CLI command to generate model in Angular 9

To generate various schematics or collections in angular the followingΒ ng generateΒ command can be used.

ng generate <schematic> [options]

(OR)

ng g <schematic> [options]

The <schematic> can take one of the following sub-commands.

- appShell
- application
- class
- component
- directive
- enum
- guard
- interceptor
- interface
- library
- module
- pipe
- service
- serviceWorker
- webWorker

Do you think that there is no way to generate angular models automatically through angular-CLI command since there is no <schematic> option available for model ?

Note, model is a class. 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.

Generate model in Angular 9

Let’s say I have the following angular application project structure.

πŸ“‚src
| β€” πŸ“‚ app
| β€” | β€” πŸ“‚ recipes
| β€” | β€” | β€” πŸ“œ recipe.model.ts
| β€” | β€” | β€” πŸ“œ recipes.component.html
| β€” | β€” | β€” πŸ“œ recipes.component.ts
| β€” | β€” | β€” πŸ“œ recipes.component.css
| β€” | β€” πŸ“‚ app.component.html
| β€” | β€” πŸ“‚ app.component.ts
| β€” | β€” πŸ“‚ app.component.css
| β€” | β€” πŸ“‚ app.component.spec.ts

Note, I already have an angular model called “recipe.model.ts“. Let’s say I wanted to generate another model called “meal.model.ts” automatically inside recipes folder through angular-CLI command.

Then run the followingΒ ng generateΒ class command with –type=model. This command will create meal model typescript file and it’s spec.ts files automatically as shown below.

> ng generate class recipes/meal --type=model

CREATE src/app/recipes/meal.model.spec.ts (152 bytes)
CREATE src/app/recipes/meal.model.ts (22 bytes)

The meal model class will have the following lines by default.

export class Meal {

}

And the meal.model.spec.ts will have the following code by default.

import { Meal } from './meal.model';

describe('Meal', () => {
  it('should create an instance', () => {
    expect(new Meal()).toBeTruthy();
  });
});

That’s it you had learnt how to use Angular CLI command to generate model in Angular 9 application.

Hope it helped πŸ™‚

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments