← Back to Angular, page 8

angular-0151 · Easy fresher

What is a decorator in Angular, what metadata does @Component carry, and how does Angular use it?

A decorator is a function written with @ above a class, property or method. Angular decorators such as @Component, @Injectable, @Directive and @Pipe attach metadata (selector, template, imports, providers) that the Angular compiler reads to know how to build and use the class.

Full answer

A decorator is a special function that you write with an @ sign directly above a class, a property, a method or a parameter. It adds information to the thing it decorates, or changes it. Decorators are a TypeScript feature, and Angular uses them to attach metadata to your code.

Metadata means data about the class. A plain class StudentCardComponent is only a TypeScript class. The decorator @Component({ ... }) tells Angular that it is a component, and describes it:

  • selector: the tag name used in templates, such as app-student-card.
  • template or templateUrl: the HTML of the component.
  • styles or styleUrl: the CSS that belongs to it.
  • imports: the other components, directives and pipes used in the template.
  • providers: services created for this component.
  • changeDetection: Default or OnPush.
  • host: bindings and listeners on the host element.

The other main class decorators are @Injectable (a service that can be injected), @Directive (behaviour for an element), @Pipe (a transform for templates) and @NgModule (legacy modules). Older code also uses property decorators: @Input, @Output, @ViewChild, @HostBinding, @HostListener. In new code, input(), output(), viewChild() and the host metadata replace most of them.

How Angular uses it: during the build, the Angular compiler reads the decorator and generates static definition code (such as the component definition with its template function) on the class. This is the ahead-of-time (AOT) compile. At run time Angular uses that generated code, not the decorator call itself. This is also why the metadata must be written in a way the compiler can read, for example plain object literals.

A decorator that you write yourself is just a function that receives the class (or the member). Angular does not need it, but it shows the idea: a function that runs once when the class is defined.

typescript
import { Component, Injectable } from '@angular/core';

// A decorator is a function. This one runs once when the class is defined.
export function Tag(label: string) {
  return (target: { tag?: string }): void => {
    target.tag = label; // a static property on the class
  };
}

@Injectable({ providedIn: 'root' })
export class MarksService {
  total(marks: number[]): number {
    return marks.reduce((sum, m) => sum + m, 0);
  }
}

@Tag('student-card')
@Component({
  selector: 'app-student-card',
  template: `<p>{{ name }} scored {{ total }}</p>`,
  styles: [`p { margin: 0; }`],
})
export class StudentCardComponent {
  static tag?: string;
  name = 'Asha';
  total = new MarksService().total([40, 35, 20]);
}

Angular 20.3 with `experimentalDecorators` enabled (the Angular CLI default), type-checked. A Karma test checks the custom decorator effect and that the component renders its template.

Key points

  • A decorator is a function used with `@` above a class, property or method.
  • Angular decorators carry metadata: selector, template, imports, providers and more.
  • The compiler turns the metadata into generated definitions on the class.
  • Property decorators like `@Input` have signal-based replacements such as `input()`.

Common mistakes

  • Forgetting the decorator and expecting a plain class to work as a component or service.
  • Putting a component in `imports` without it being standalone.
  • Writing metadata with values that the compiler cannot read at build time.

Follow-ups

  • What is the difference between a decorator and a TypeScript class?
  • Why does Angular generate code from decorators at build time?
  • Which decorators have signal based replacements?

Tips

  • Say "metadata" and list four fields from memory.
  • Mention that the AOT compiler reads the decorator.
  • Show awareness that newer APIs reduce decorator use.

Evaluation rubric

  • Defines a decorator correctly.
  • Lists common decorators and metadata fields.
  • Explains the build time role.