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
@Componentdecorator 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
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
5. The Template (HTML)
Insideuser-profile.component.html, we can now use the public properties from the TypeScript class using interpolation ({{ }}):
html
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
7. Component Architecture Visual
text
8. Standalone Components (Modern Angular)
Modern Angular (v17+) promotes Standalone Components, which removes the need for NgModules. When generating, you can use:
bash
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
declarationsarray insideapp.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
UserProfilecomponent 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
html
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?
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
templateUrlandtemplatein the@Componentdecorator?
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.