angular
/

Angular Components – The Building Blocks of UI

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

angular

Angular Components – The Building Blocks of UI

What is a Component?

In Angular, a Component is the most fundamental building block of the user interface. It is a self-contained unit that controls a patch of the screen called a 'view'. As an Architect, you should view components as encapsulated modules that combine logic, layout, and styles. This separation ensures that your code remains testable and scalable, similar to the widget-based architecture in Flutter.

  1. The Three Parts of a Component

Every Angular component consists of three distinct parts that work together to define its behavior and appearance.

  • The Component Class: A TypeScript class that handles data and logic (e.g., fetching project details).
  • The HTML Template: Defines the visual structure of the component.
  • The CSS Styles: Defines the private styles scoped specifically to that component.

  1. The @Component Decorator

The @Component decorator is a function that identifies the class immediately below it as an Angular component. It provides the Metadata that tells Angular where to find the template and styles, and what selector to use in HTML.

TypeScriptRead-only
1
import { Component } from '@angular/core';

@Component({
  selector: 'app-project-card',           // How to use it: <app-project-card></app-project-card>
  templateUrl: './project-card.component.html',
  styleUrls: ['./project-card.component.scss']
})
export class ProjectCardComponent {
  projectName: string = 'Revochamp';
  isFeatured: boolean = true;
}

  1. Component Communication: Input and Output

To build a truly modular system, components must talk to each other. We use the @Input() decorator to pass data from a parent to a child, and the @Output() decorator with an EventEmitter to send data back up.

TypeScriptRead-only
1
// Inside Child Component
@Input() title: string = '';
@Output() onAction = new EventEmitter<void>();

// Inside Parent Template
<app-project-card 
  [title]="'Flutter Engine'"
  (onAction)="handleUpdate()">
</app-project-card>

  1. Component Lifecycle Hooks

Angular manages the life of a component from creation to destruction. You can 'tap into' specific moments in this lifecycle using Lifecycle Hooks. For a Lead Developer, ngOnInit is the most common place to initialize data or call APIs.

HookTimingBest For
ngOnChangesBefore ngOnInitResponding to @Input() data changes
ngOnInitAfter first checkInitializing component data / API calls
ngAfterViewInitAfter view rendersAccessing child DOM elements or ViewChild
ngOnDestroyJust before removalCleaning up subscriptions & timers

Test Your Knowledge

Q1
of 3

Which decorator is used to pass data from a Parent component to a Child component?

A
@Output()
B
@Injectable()
C
@Input()
D
@ViewChild()
Q2
of 3

Where is the best place to perform an initial API call in an Angular component?

A
The constructor
B
ngOnInit
C
ngOnDestroy
D
index.html
Q3
of 3

What does the 'templateUrl' property in the @Component decorator do?

A
It defines the CSS styles for the component
B
It sets the URL for the external API
C
It points to the HTML file that defines the component's view
D
It registers the component in the root module

Frequently Asked Questions

What is a 'Selector' in a component?

A selector is the CSS tag name you assign to your component. When Angular finds this tag in your HTML (e.g., <app-header>), it knows to render the component's template at that location.

What are 'Standalone Components'?

In modern Angular (v14+), standalone components allow you to build components without needing to declare them in an NgModule. This reduces boilerplate and is the current industry standard for new projects.

Can I use external libraries inside a component?

Yes. You can import any TypeScript/JavaScript library into your component class. For example, you could import the Gemini API client directly into a component to handle AI generation logic.

Previous

angular project structure

Next

angular templates

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.