← Back to Articles         
What is a component directive?

What is a component directive?

Published on 29 Sep 2026     5 min read Angular
Directives

What is a Component Directive in Angular?

1. What is a Component Directive?

A Component Directive is an Angular directive that has its own template (HTML view).

In Angular, a component is actually a special type of directive. It is created using the @Component decorator.

@Component({
  selector: 'app-user',
  template: `<h2>Hello User</h2>`
})
export class User {
}

The main difference is:

Component = Directive + Template

A component controls a section of the UI, whereas other directives normally modify the behavior or appearance of existing DOM elements.


2. Why Do We Need Component Directives?

Angular applications are built by dividing the UI into small, reusable components.

For example:

Application
│
├── Header
├── Sidebar
├── ProductList
│   ├── Product
│   ├── Product
│   └── Product
└── Footer

Each section can be implemented as a separate component.

This makes applications easier to develop, reuse, test, and maintain.


3. Full Simple Sample Program

Consider a simple UserCard component.

user-card.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-user-card',
  standalone: true,
  templateUrl: './user-card.html',
  styleUrl: './user-card.css'
})
export class UserCard {

  name = 'Syed Mohamed';
  role = 'Technical Lead';

}

user-card.html

<div class="card">
  <h2>{{ name }}</h2>
  <p>{{ role }}</p>
</div>

user-card.css

.card {
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 8px;
}

app.ts

import { Component } from '@angular/core';
import { UserCard } from './user-card';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [UserCard],
  templateUrl: './app.html'
})
export class App {
}

app.html

<h1>Employee Details</h1>

<app-user-card></app-user-card>

4. Explanation

@Component

@Component({...})

The @Component decorator tells Angular that the class represents a component.

selector

selector: 'app-user-card'

Defines the custom HTML element used to display the component:

<app-user-card></app-user-card>

templateUrl

templateUrl: './user-card.html'

Specifies the HTML template controlled by the component.

This is the important characteristic that distinguishes a component from an ordinary attribute or structural directive.

styleUrl

styleUrl: './user-card.css'

Specifies the styles associated with the component.

Component Class

export class UserCard {
  name = 'Syed Mohamed';
  role = 'Technical Lead';
}

The class contains the data and behavior used by the template.

The template accesses those properties using interpolation:

<h2>{{ name }}</h2>
<p>{{ role }}</p>

5. Component Directive vs Other Directives

Angular directives are commonly classified into three types:

Type Purpose Example
Component directive Creates a UI with its own template @Component
Attribute directive Changes appearance/behavior of an element ngClass, ngStyle
Structural directive Changes DOM structure *ngIf, *ngFor

For example:

<app-user-card></app-user-card>

app-user-card represents a component.

Whereas:

<p [ngClass]="myClass">Hello</p>

ngClass is an attribute directive.

And traditional structural-directive syntax looks like:

<div *ngIf="isLoggedIn">
  Welcome
</div>

Modern Angular also provides built-in control-flow syntax such as @if and @for, which is generally preferred for new code.


6. Advantages

  • Reusable UI — the same component can be used in multiple places.
  • Separation of concerns — UI can be divided into smaller units.
  • Maintainability — easier to modify one part of a large application.
  • Testability — individual components can be tested separately.
  • Encapsulation — template, styles, data, and behavior belong to one logical UI unit.
  • Composition — large screens can be constructed from smaller components.

7. Disadvantages

Components themselves are fundamental to Angular, but excessive component splitting can introduce problems:

  • Too many tiny components can make the application structure harder to navigate.
  • Parent-child communication can become more complex in deeply nested component trees.
  • Poor component design can create unnecessary dependencies between components.

Therefore, create components around meaningful and reusable UI responsibilities.


8. Key Points

  • A component is a special type of directive.
  • A component is created using @Component.
  • A component has its own template.
  • Components are the primary building blocks of an Angular application's UI.
  • Every component requires a selector that Angular can use to identify where it should render.
  • A component can contain template + styles + TypeScript logic.
  • In modern Angular, components are standalone by default.
  • A normal @Directive does not have its own template.

Easy way to remember

Directive
   │
   ├── Component Directive
   │       └── Has its own template
   │
   ├── Attribute Directive
   │       └── Changes appearance/behavior
   │
   └── Structural Directive
           └── Changes DOM structure

9. Interview Questions and Answers

Q1. What is a component directive in Angular?

A component directive is a special type of directive that has its own template. It is created using the @Component decorator and represents a reusable part of the application's UI.

Q2. Is every Angular component a directive?

Yes. A component is a specialized directive with a template.

Q3. Is every directive a component?

No. Attribute and structural directives do not have their own component templates.

Q4. What is the main difference between a component and a directive?

The main difference is the template.

Component → Has its own template
Directive → Normally modifies an existing element/behavior

Q5. Which decorator is used to create a component?

@Component()

Q6. Which decorator is used to create a custom non-component directive?

@Directive()

Q7. Can a component have its own CSS?

Yes. A component can define styles using styles, styleUrl, or styleUrls.

Q8. What is the simplest definition for an interview?

A component is a special type of Angular directive that has its own template and is used to create reusable UI elements.

← Back to Articles