Skip to main content

Overview

The @Component decorator marks a class as an Angular component and provides configuration metadata. Components are the most basic UI building blocks of an Angular application.

Signature

Parameters

Component
required
Configuration object that specifies how the component should be processed, instantiated, and used at runtime.

Component Metadata

selector

string
CSS selector that identifies this component in a template. Can be an element name, class, attribute, or combination.

template / templateUrl

string
Inline HTML template for the component.
string
Path to an external HTML template file.
Use either template or templateUrl, not both.

styles / styleUrls / styleUrl

string | string[]
Inline CSS styles for the component.
string[]
Paths to external CSS stylesheet files.
string
Path to a single external CSS stylesheet file.

standalone

boolean
default:"true"
When true, the component does not need to be declared in an NgModule. When false, it must be declared in an NgModule’s declarations array.

imports

Type<any> | ReadonlyArray<any>
Standalone component dependencies - other components, directives, pipes, or NgModules that can be used in the template.
Only available for standalone components.

providers

Provider[]
Services or values available for dependency injection in this component and its children.

viewProviders

Provider[]
Services available only to the component’s view (not content children).

changeDetection

ChangeDetectionStrategy
Strategy for detecting changes:
  • ChangeDetectionStrategy.Default - Check component on every change detection cycle
  • ChangeDetectionStrategy.OnPush - Check only when inputs change or events fire

encapsulation

ViewEncapsulation
Style encapsulation strategy:
  • ViewEncapsulation.Emulated - Emulate shadow DOM (default)
  • ViewEncapsulation.None - No encapsulation
  • ViewEncapsulation.ShadowDom - Use native shadow DOM

inputs

string[] | {name: string, alias?: string, required?: boolean, transform?: Function}[]
Array of input property names or configuration objects.

outputs

string[]
Array of output property names.

exportAs

string
Name for template variable references.
Usage: <app-user-profile #profile="userProfile"></app-user-profile>

host

{[key: string]: string}
Map of class properties to host element bindings.

hostDirectives

Type<unknown>[] | {directive: Type<unknown>, inputs?: string[], outputs?: string[]}[]
Directives to apply to the host element.

Basic Example

user-profile.component.ts

Component with Inputs and Outputs

counter.component.ts

OnPush Change Detection

optimized.component.ts
With OnPush, the component only checks for changes when:
  • An input reference changes
  • An event originates from the component or its children
  • Change detection is manually triggered

Component with External Files

app.component.ts

See Also

Components Guide

Learn component fundamentals

Templates

Master template syntax

Styling

Style your components

Change Detection

Optimize performance