Component Anatomy
An Angular component is made up of different parts that work together to create a user interface. Understanding these parts helps us create and maintain Angular applications effectively.
What is Component Anatomy?
Component anatomy means understanding the different parts of an Angular component and how those parts work together.
A component mainly contains a TypeScript class, a component decorator, a template and styling.
Angular Component Anatomy
Angular Component
Complete UI building block
@Component
Provides metadata that tells Angular how to configure the component.
TypeScript Class
Contains properties, methods and application logic.
HTML Template
Defines the user interface displayed in the browser.
Styles
Defines the appearance of the component.
Complete Component Example
The following example shows the basic structure of a Student component.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrl: './student.component.css'
})
export class StudentComponent {
studentName = 'Sam';
displayMessage() {
return 'Welcome ' + this.studentName;
}
}
1. Import Statement
The component uses the Angular Component decorator. It is imported from Angular core.
import { Component } from '@angular/core';
Purpose
The import statement makes the Component decorator available to the TypeScript class.
2. @Component Decorator
The @Component decorator provides metadata about the component.
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrl: './student.component.css'
})
3. selector
The selector defines the custom HTML element through which the component can be used.
selector: 'app-student'
The component can be placed inside another template using:
<app-student></app-student>
4. templateUrl
The templateUrl property specifies the external HTML file used by the component.
templateUrl: './student.component.html'
The HTML template can contain Angular interpolation, property binding, event binding and other template features.
5. styleUrl
The styleUrl property specifies the stylesheet associated with the component.
styleUrl: './student.component.css'
6. Component Class
The TypeScript class contains the data and logic required by the component.
export class StudentComponent {
studentName = 'Sam';
displayMessage() {
return 'Welcome ' + this.studentName;
}
}
Property
studentName
Stores component data.
Method
displayMessage()
Performs component logic.
7. Component Template
The HTML template defines what the user sees in the browser.
<h2>Student Details</h2>
<p>
Student Name: {{ studentName }}
</p>
<p>
{{ displayMessage() }}
</p>
8. Component Styling
Component-specific styles can be written in the component stylesheet.
h2 {
color: #4c1d95;
}
p {
font-size: 18px;
}
Expected Output
Student Details
Student Name: Sam
Welcome Sam
How Component Parts Work Together
Component Decorator
Defines component metadata.
TypeScript Class
Provides data and logic.
HTML Template
Displays the data.
Browser
Shows the final UI.
Component File Relationship
Summary
Angular component anatomy consists of the component decorator, TypeScript class, HTML template and styling.
The decorator defines component metadata such as selector, templateUrl and styleUrl.
The TypeScript class contains the component logic, while the HTML template displays the data to the user.