Skip to main content
Angular Basics
CHAPTER 05 Beginner

Angular Components

Updated: May 18, 2026
5 min read

# CHAPTER 5

Angular Components

1. Chapter Introduction

A Component is the most fundamental building block of any Angular application. Think of your application as a house: the house itself is one component, the kitchen is a component, each room is a component, and even the light switches are components. Each component is a self-contained unit consisting of its own logic (TypeScript), its own view (HTML), and its own styling (CSS). When you combine all these components together, you get a complete user interface.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Generate a component using the Angular CLI.
  • Understand the four files that make up a component.
  • Understand the @Component decorator and its properties.
  • Use a component inside another component (nesting).
  • Build a real User Profile component.

3. Generating a Component

The fastest way to create a new component is with the Angular CLI. Open a terminal inside your project folder and run:
bash
12345
# Full command
ng generate component user-profile

# Shorthand
ng g c user-profile

The CLI will automatically create 4 files inside a new user-profile folder:

  • user-profile.component.ts — The TypeScript logic.
  • user-profile.component.html — The HTML template.
  • user-profile.component.css — The encapsulated styles.
  • user-profile.component.spec.ts — The unit test file.

4. The @Component Decorator

When you open the .ts file, you will see a special @Component({...}) block above the class. This is a Decorator. Decorators are special functions that attach metadata to a class, telling Angular how to treat it.
typescript
12345678910111213
import { Component } from '@angular/core';

@Component({
  selector: 'app-user-profile',    // The HTML tag name to use this component
  templateUrl: './user-profile.component.html', // Link to the HTML file
  styleUrls: ['./user-profile.component.css']   // Link to the CSS file
})
export class UserProfileComponent {
  // TypeScript logic goes here
  name: string = 'Alice Johnson';
  role: string = 'Senior Developer';
  avatarUrl: string = 'assets/avatar.png';
}

5. The Template (HTML)

Inside user-profile.component.html, we can now use the public properties from the TypeScript class using interpolation ({{ }}):
html
123456
<!-- user-profile.component.html -->
<div class="profile-card">
  <img [src]="avatarUrl" alt="User Avatar" />
  <h2>{{ name }}</h2>
  <p>{{ role }}</p>
</div>

6. Using a Component (The selector)

The selector property (app-user-profile) defines the custom HTML tag for this component. To display the User Profile card inside the main app.component.html, just add the tag:
html
12345
<!-- app.component.html -->
<h1>Welcome to our Platform</h1>

<!-- Angular renders the entire user-profile component here! -->
<app-user-profile></app-user-profile>

7. Component Architecture Visual

text
123456789
App Component (app.component.html)
   |
   +---- <app-user-profile>
   |         |
   |         +-- name: "Alice"
   |         +-- role: "Developer"
   |         +-- avatarUrl: "assets/..."
   |
   +---- <app-footer>

8. Standalone Components (Modern Angular)

Modern Angular (v17+) promotes Standalone Components, which removes the need for NgModules. When generating, you can use:
bash
1
ng g c user-profile --standalone

A standalone component includes its own imports array inside the @Component decorator, making it fully self-sufficient.

9. Common Mistakes

  • Forgetting to declare the component: In older Angular (non-standalone), if you manually create a component file without using the CLI, you must manually add it to the declarations array inside app.module.ts. The CLI does this automatically.
  • Selector typos: If you write <app-userprofile> in the template instead of <app-user-profile>, Angular will render nothing and throw a warning.

10. Best Practices

  • One component, one responsibility: A UserProfile component should only show user data. It should not fetch data from an API. Data fetching belongs in Services (Chapter 9).
  • Smart vs Dumb Components: "Dumb" components receive data as inputs and only display it. "Smart" components fetch and manage state. Keep most components "dumb".

11. Mini Project: User Profile Component

user-profile.component.ts
1234567891011121314
import { Component } from &#039;@angular/core&#039;;

@Component({
  selector: &#039;app-user-profile&#039;,
  templateUrl: &#039;./user-profile.component.html&#039;,
  styleUrls: [&#039;./user-profile.component.css&#039;]
})
export class UserProfileComponent {
  name: string = &#039;Alice Johnson&#039;;
  role: string = &#039;Senior Angular Developer&#039;;
  followers: number = 1240;
  following: number = 382;
  bio: string = &#039;Passionate about building scalable Angular applications.&#039;;
}
html
1234567891011
<!-- user-profile.component.html -->
<div class="card">
  <div class="avatar">AJ</div>
  <h2>{{ name }}</h2>
  <span class="badge">{{ role }}</span>
  <p>{{ bio }}</p>
  <div class="stats">
    <div><strong>{{ followers }}</strong> Followers</div>
    <div><strong>{{ following }}</strong> Following</div>
  </div>
</div>

12. MCQs with Answers

Question 1

What is the most fundamental building block of an Angular application?

Question 2

What Angular CLI command generates a new component called product-card?

Question 3

What is the purpose of the @Component decorator?

Question 4

What does the selector property in @Component define?

Question 5

How do you display a TypeScript property called title inside an HTML template?

Q6. Is CSS written in my-component.component.css applied globally to the entire application? a) Yes b) No, it is encapsulated and only applies to that specific component's template. Answer: b) No, it is encapsulated.
Question 7

What modern Angular feature allows a component to be fully self-sufficient without belonging to an NgModule?

Question 8

Where does a component's business logic (TypeScript properties, methods) live?

Question 9

What is the principle of "Smart vs Dumb" components?

Question 10

If you add <app-product-card></app-product-card> to a template but nothing shows, what is the most common cause?

13. Interview Questions

  • Q: Explain the purpose of each of the four files generated when you create an Angular component with the CLI.
  • Q: What is the difference between templateUrl and template in the @Component decorator?

14. Summary

Components are the soul of Angular. By splitting every part of your UI into small, isolated, and reusable components, you can build applications of incredible complexity without the code turning into an unmaintainable mess. The @Component decorator is the bridge that transforms a plain TypeScript class into a living, breathing piece of UI.

15. Next Chapter Recommendation

Our components look empty. In Chapter 6: Templates and Data Binding, we will bring them to life by learning how to bind TypeScript data to HTML elements and react to user interactions like button clicks.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·