Your First Component and Bootstrapping

Angular 12 min min read Updated: Nov 16, 2026 Beginner
Your First Component and Bootstrapping
Beginner Topic 4 of 5

What Is a Component?

A component is a small, reusable piece of the screen: a header, a product card, a login form. It has three parts: TypeScript (data and behavior), an HTML template (what is shown) and CSS (how it looks).

Simple example: Lego. A complete page is a model built from small bricks (components). You can reuse the same brick many times and change one without breaking the rest.

Generate a Component

bash g generate component greeting # short form ng g c greeting

The CLI creates four files in src/app/greeting/ (names depend on your CLI version):

text greeting.ts greeting.html greeting.css greeting.spec.ts
typescript // greeting.ts import { Component } from '@angular/core'; @Component({ selector: 'app-greeting', imports: [], templateUrl: './greeting.html', styleUrl: './greeting.css', }) export class Greeting { name = 'Ana'; }
html <!-- greeting.html --> <h2>Hello, {{ name }}!</h2>

Use It in Another Component

Two steps: import the class and use the selector in the template.

typescript // app.ts import { Component } from '@angular/core'; import { Greeting } from './greeting/greeting'; @Component({ selector: 'app-root', imports: [Greeting], // step 1 templateUrl: './app.html', }) export class App {}
html <!-- app.html --> <app-greeting /> <!-- step 2 --> <app-greeting /> <!-- reuse it -->
Result

Hello, Ana! Hello, Ana!

Common error: 'app-greeting' is not a known element. You forgot to add the class to the imports array of the component that uses it.

Standalone Components

Since Angular 19 components are standalone by default: they declare their own dependencies in imports and do not need an NgModule. Older projects have a line standalone: false or use modules. New code should always be standalone.

Inline Template and Styles

typescript @Component({ selector: 'app-badge', template: `<span class="badge">{{ label }}</span>`, styles: `.badge { background: #0d6efd; color: white; padding: 2px 8px; border-radius: 6px; }`, }) export class Badge { label = 'New'; }

Inline code is nice for tiny components. Use separate files for bigger ones.

The Component Tree

text App |- Header |- ProductList | |- ProductCard | |- ProductCard |- Footer

Every Angular app is a tree of components. Data flows down with inputs and events flow up with outputs (a later lesson).

Naming Rules

  • Selector uses a prefix and kebab-case: app-user-card
  • Class names use PascalCase: UserCard
  • One component per file, one folder per feature

Conclusion

A component bundles TypeScript, HTML and CSS. Generate it with ng g c, add it to imports of the parent and use its selector like a custom HTML tag.

Quick Summary: ng g c name; imports: [Name]; <app-name />.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators