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:
- Angular 9 img src binding in ngFor loop : Example
- Add a Responsive Bootstrap Navigation Bar in Angular 9
- Best way to delete components in Angular 9 with CLI ?
- HTML Property Binding in Angular : Data Binding
- Global Angular CLI version is greater than your local version
- Create custom events and fire in Angular 9 with EventEmitter β Example
- ERROR in multi /bootstrap.min.css ./src/styles.css in Angular 9
- Bind selected element from drop down to an object in Angular 9
- Create common app-header component in Angular 9
- How to stop generation of .spec.ts test files using Angular CLI ?
- Emmet in Visual Studio Code not working ?
- Angular: Quickly create div classes in Visual Studio Code editor
- Error Type cannot be used as an index type