Standalone Components
Standalone Components are Angular components that can work without being declared inside an NgModule. They provide a simpler and more direct way to organize modern Angular applications.
What is a Standalone Component?
A standalone component is a component configured with
standalone: true. Its required dependencies can be
imported directly through the component's imports
property.
Simple Definition
A standalone component can manage its own template dependencies without requiring the component to be declared in an NgModule.
Traditional vs Standalone Components
- Uses NgModules.
- Components are declared in modules.
- Dependencies are commonly configured at module level.
- More configuration may be required.
- Uses
standalone: true. - No component declaration in an NgModule is required.
- Dependencies can be imported directly.
- Provides a simpler component structure.
Standalone Component Flow
User
Interacts with application
Standalone Component
Own configuration
Template
Displays UI
Browser
Rendered interface
Creating a Standalone Component
The following example creates a simple standalone component for displaying student information.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
standalone: true,
template: `
<h2>Student Details</h2>
<p>Student Name: Sam</p>
`
})
export class StudentComponent {
}
Student Details
Student Name: Sam
The standalone Property
The standalone metadata property tells Angular that the
component is standalone.
@Component({
selector: 'app-student',
standalone: true
})
Important
Standalone components manage their template dependencies directly instead of requiring component declaration through an NgModule.
Using the imports Property
Standalone components can specify the dependencies needed by their
templates through the imports property.
@Component({
selector: 'app-student',
standalone: true,
imports: [],
template: `
<h2>Student Details</h2>
`
})
export class StudentComponent {
}
When the template requires a standalone component, directive or pipe,
the required item can be added to the imports array.
Standalone Component with External Template
A standalone component can also use an external HTML template.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
standalone: true,
templateUrl: './student.component.html',
styleUrl: './student.component.css'
})
export class StudentComponent {
studentName = 'Pradnya';
}
<h2>Student Details</h2>
<p>Student Name: {{ studentName }}</p>
Student Details
Student Name: Pradnya
Creating a Standalone Component Using CLI
Angular CLI can be used to generate components.
ng generate component student
Short form:
ng g c student
Standalone Component File Structure
student/
│
├── student.component.ts
├── student.component.html
├── student.component.css
└── student.component.spec.ts
student.component.ts
Contains component class and metadata.
student.component.html
Contains the component template.
student.component.css
Contains component-specific styles.
student.component.spec.ts
Contains component testing code.
Benefits of Standalone Components
Simpler Structure
Components can manage their own dependencies directly.
Less Configuration
Reduces the need for module-level component declarations.
Reusable
Standalone components can be imported where they are needed.
Clear Dependencies
Template dependencies can be visible in the component.
Complete Standalone Component Example
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
standalone: true,
template: `
<div class="student-card">
<h2>Student Profile</h2>
<p>Name: Sam</p>
<p>Course: Angular Full Stack</p>
</div>
`,
styles: [`
.student-card {
padding: 20px;
border-radius: 8px;
}
h2 {
margin-bottom: 10px;
}
`]
})
export class StudentComponent {
}
Student Profile
Name: Sam
Course: Angular Full Stack
Complete Standalone Component Flow
Common Mistakes
❌ Forgetting standalone configuration
When creating a standalone component explicitly, make sure its configuration is correct.
❌ Missing template dependencies
If a template uses a standalone component, directive or pipe, the required dependency needs to be available to the component.
❌ Confusing component metadata with component logic
Metadata configures the component, while the component class contains its application logic.
Best Practices
- Keep standalone components focused on a specific responsibility.
- Keep component dependencies clear through the imports property.
- Use meaningful selectors.
- Keep templates readable and maintainable.
- Keep component-specific styling close to the component.
- Use reusable standalone components for common UI functionality.
Summary
Standalone Components are Angular components that can be configured independently without requiring component declaration inside an NgModule.
The standalone property enables standalone behavior, while the imports property provides dependencies required by the component template.
Standalone components help create a simpler, clearer and more modular Angular application structure.