Standalone components let you create Angular components without needing a module. This makes your code simpler and easier to manage.
Standalone component declaration in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
import { Component } from '@angular/core'; @Component({ selector: 'app-example', standalone: true, template: `<p>Hello from standalone component!</p>`, imports: [] // optional: other standalone components or directives }) export class ExampleComponent {}
The key part is standalone: true inside the @Component decorator.
You can add other standalone components or directives in the imports array if needed.
import { Component } from '@angular/core'; @Component({ selector: 'app-simple', standalone: true, template: `<h1>Simple Standalone</h1>` }) export class SimpleComponent {}
CommonModule to use Angular features like event binding.import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-with-common', standalone: true, imports: [CommonModule], template: `<button (click)="count = count + 1">Clicked {{count}} times</button>` }) export class WithCommonComponent { count = 0; }
This standalone component shows a counter. It uses Angular signals to track the count and updates the display when the button is clicked.
import { Component, signal } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-counter', standalone: true, imports: [CommonModule], template: ` <h2>Counter</h2> <button (click)="increment()">Increment</button> <p>Count: {{ count() }}</p> ` }) export class CounterComponent { count = signal(0); increment() { this.count.set(this.count() + 1); } }
Standalone components do not need to be declared in any NgModule.
You can use imports to bring in other standalone components or Angular modules.
Standalone components help reduce boilerplate and improve app modularity.
Standalone components simplify Angular by removing the need for NgModules.
Use standalone: true in the component decorator to declare them.
They can import other standalone components or modules via the imports array.
Practice
standalone: true in an Angular component's decorator do?Solution
Step 1: Understand Angular component declaration
Normally, Angular components must be declared inside an NgModule to be usable.Step 2: Effect of
Settingstandalone: truestandalone: trueallows the component to be used without declaring it in any NgModule.Final Answer:
Declares the component as standalone, removing the need for NgModule declaration. -> Option DQuick Check:
Standalone component = no NgModule needed [OK]
- Thinking standalone makes component lazy-loaded
- Assuming standalone registers component globally
- Confusing standalone with change detection settings
Solution
Step 1: Check the standalone property usage
The standalone property must be set to true inside the @Component decorator.Step 2: Verify template declaration and class export
The template can be inline withtemplateor external withtemplateUrl. The class must be exported properly.Final Answer:
@Component({ selector: 'app-test', standalone: true, template: '<p>Test</p>' }) export class TestComponent {} -> Option AQuick Check:
Standalone true inside @Component with inline template [OK]
- Setting standalone to false
- Declaring template outside the decorator
- Mixing NgModule declaration with standalone component
import { Component } from '@angular/core';
@Component({
selector: 'app-hello',
standalone: true,
template: `Hello, {{ name }}!
`
})
export class HelloComponent {
name = 'Angular';
}Solution
Step 1: Understand template interpolation
The template uses {{ name }} which Angular replaces with the component's property value.Step 2: Check the property value
The component definesname = 'Angular', so interpolation outputs 'Angular'.Final Answer:
<h1>Hello, Angular!</h1> -> Option CQuick Check:
Template interpolation replaces {{ name }} with 'Angular' [OK]
- Thinking interpolation shows raw {{ name }}
- Assuming standalone disables property binding
- Expecting runtime error for missing property
@Component({
selector: 'app-sample',
standalone: true,
template: 'Sample
',
imports: [CommonModule]
})
export class SampleComponent {}Solution
Step 1: Check imports usage in standalone component
Standalone components can import modules like CommonModule via the imports array in the decorator.Step 2: Verify import statement presence
The code uses CommonModule in imports but does not import it from '@angular/common' at the top.Final Answer:
Missing import statement for CommonModule -> Option BQuick Check:
Imports array needs proper import statements [OK]
- Thinking imports array is invalid in @Component
- Forgetting to import CommonModule from '@angular/common'
- Assuming CommonModule cannot be used in standalone components
ButtonComponent. How should you declare the imports array in your component decorator?Solution
Step 1: Understand how to use other standalone components
Standalone components can import other standalone components by listing them in the imports array.Step 2: Correct syntax for imports array
The imports array must contain the component class itself, not a string or NgModule.Final Answer:
@Component({ standalone: true, imports: [ButtonComponent], template: '<app-button></app-button>' }) -> Option AQuick Check:
Imports array includes component classes, not strings [OK]
- Using string names instead of component classes in imports
- Importing NgModule instead of component
- Omitting imports array when using other standalone components
