In Angular, components are the core building blocks of the application UI. Every screen, header, button group, or reusable UI section is typically built as a component.
Each Angular component is made up of three main parts:
?️ Building Block of Angular UI
<app-root>).The basic structure of an Angular component uses the @Component decorator from @angular/core and a TypeScript class:
// Basic Angular component structure
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
template: '<h2>Example Component</h2>',
styles: ['h2 { color: purple; }']
})
export class ExampleComponent {
message = 'Hello from ExampleComponent!';
}
Here is what each part means:
selector: The tag name you will use in HTML to display this component.template: Inline HTML content (you can also use templateUrl for an external file).styles: Inline CSS rules (or styleUrls for external CSS files).ExampleComponent: The class that holds data and logic for the component.Let’s look at a simple root component that Angular creates by default in a new project.
// app.component.ts (root component)
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: '<h1>Welcome to Angular Components</h1>',
styles: ['h1 { color: darkblue; }']
})
export class AppComponent {
title = 'My Angular App';
}
When the application runs, Angular finds the <app-root> tag in index.html and replaces it with the component’s template:
AppComponent class can expose properties (like title) that are used in the template.You can generate new components using the Angular CLI. This automatically creates all the required files and updates the module declarations for you.
// Generate a new component using Angular CLI
ng generate component my-component
// Shorthand command
ng g c my-component
These commands create a folder my-component/ with four files:
my-component.component.ts → TypeScript logic (class + decorator)my-component.component.html → Template (view)my-component.component.css → Styles (scoped CSS)my-component.component.spec.ts → Testing file (unit tests)Now let’s build a small reusable component that greets the user by name.
// my-component.component.ts (logic + metadata)
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent {
name = 'Student';
}
<!-- my-component.component.html (template) -->
<h2>Hello, {{ name }}! Welcome to Angular.</h2>
/* my-component.component.css (styles) */
h2 {
color: teal;
font-weight: 600;
}
When you place the component’s selector inside another template (for example, in app.component.html):
<!-- app.component.html -->
<h1>My Angular App</h1>
<app-my-component></app-my-component>
Angular will render:
name in MyComponent, the message updates automatically on the screen.user-profile, navbar, or product-card.users/, auth/, dashboard/).templateUrl and styleUrls) for better readability on larger components.@Component decorator.greeting that displays your own name and a welcome message.<app-greeting></app-greeting> selector multiple times in app.component.html to see how components are reusable.courseName to the component class and bind it in the template using interpolation.