Sub topic 10
Generating components, services and more with ng generate
This chapter assumes you have a workspace created with ng new, know basic TypeScript, and understand that Angular apps are built from components and services. The ng generate command (alias ng g) creates these files for you with the correct structure, naming and wiring.
The working model
ng generate runs a schematic. A schematic is a recipe that writes files and sometimes edits existing ones. The command takes a schematic name and a path:
ng generate component products/product-list
ng generate service products/productThe last segment of the path is the name. Everything before it is a folder, relative to src/app. The CLI creates missing folders and places the files there.
The first command creates product-list.ts, a template, a stylesheet and a spec file. In Angular 19 and earlier, the files are named product-list.component.ts and so on, and the class is ProductListComponent. Newer CLI versions drop the suffix and name the class ProductList. Check which your version produces.
Generated components are standalone. They list their own imports instead of belonging to an NgModule, so the CLI has no module to edit.
import { Component } from '@angular/core';
@Component({
selector: 'app-product-list',
imports: [],
templateUrl: './product-list.component.html',
styleUrl: './product-list.component.css',
})
export class ProductListComponent {}The app prefix on the selector comes from the prefix setting for the project in angular.json.
Common schematics
| Command | Creates |
|---|---|
| `ng g component name` | Component with template, styles and spec |
| `ng g service name` | Injectable service, provided in root |
| `ng g directive name` | Attribute directive |
| `ng g pipe name` | Pipe for templates |
| `ng g guard name` | Route guard |
| `ng g interface name` | TypeScript interface |
Run ng generate --help to list the schematics available in your project.
Useful flags
--dry-run(or-d) lists what would be created without writing anything.--skip-testsomits the spec file.--inline-templateand--inline-styleput the template and styles inside the decorator.--flator--no-flatcontrols whether a folder is created for the item.
ng generate component products/product-card --dry-run
ng generate component products/product-card --inline-template --skip-testsCommon errors
- Running the command in the wrong folder. The CLI resolves paths from the workspace. Inside a subfolder, it uses that folder as the base. Run it from the workspace root unless you intend otherwise.
- Forgetting to import the component. A generated component is not usable in another template until you add it to that component's
importsarray. - Typing the name in the wrong case. Use kebab-case such as
product-list. The CLI converts it to the class name itself. - Expecting module edits. In standalone projects, generating does not register anything globally. Routes still go in
app.routes.ts.
Exercise
You run ng generate service cart/cart-store. Where does the CLI put the service file, and what command would show this without creating anything?
Show answer
It puts the service in src/app/cart/, creating the cart folder if it is missing. Add --dry-run (or -d) to the command to preview the files without writing them.