WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Angular Β· Basic Β· question 14 of 100

What are Angular decorators and what is their purpose?

πŸ“• Buy this interview preparation book: 100 Angular questions & answers β€” PDF + EPUB for $5

Angular decorators are a design pattern in Angular that plays a crucial role in the framework. They use the decorator pattern to add metadata to classes, methods, properties, and parameters at runtime. In short, decorators are used to extend and modify the behavior of the classes, properties, and functions they are applied to.

There are four main types of Angular decorators:

1. **Class decorators**: These decorators add metadata to a class or modify its behavior. The most commonly used class decorators in Angular are β€˜@Component()β€˜, β€˜@NgModule()β€˜, β€˜@Directive()β€˜, and β€˜@Pipe()β€˜. These decorators define a class as a component, a module, a directive, or a pipe, respectively, and add the necessary metadata to them.

Example of a β€˜@Component()β€˜ decorator:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'my-angular-project';
}

2. **Property decorators**: These decorators are applied to properties inside classes to modify their behavior or add metadata to them. The most common property decorators are β€˜@Input()β€˜, β€˜@Output()β€˜, and β€˜@ViewChild()β€˜. You can create custom property decorators as well.

Example of a β€˜@ViewChild()β€˜ decorator:

import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'app-root',
  template: `
    <app-child></app-child>
    <button (click)="onClick()">Call child method</button>`,
})
export class AppComponent {
  @ViewChild(ChildComponent) childComponent!: ChildComponent;

  onClick(): void {
    this.childComponent.childMethod();
  }
}

3. **Method decorators**: These decorators can be used to modify the behavior of a method or to wrap it with additional functionality. A common method decorator in Angular is β€˜@HostListener()β€˜, which allows you to handle events raised by the host element in a directive or component.

Example of a β€˜@HostListener()β€˜ decorator:

import { Directive, ElementRef, HostListener } from '@angular/core';

@Directive({
  selector: '[appHighlight]',
})
export class HighlightDirective {
  constructor(private el: ElementRef) {}

  @HostListener('mouseenter') onMouseEnter() {
    this.highlight('yellow');
  }

  @HostListener('mouseleave') onMouseLeave() {
    this.highlight(null);
  }

  private highlight(color: string | null) {
    this.el.nativeElement.style.backgroundColor = color;
  }
}

4. **Parameter decorators**: These decorators are applied to the parameters of a class constructor and are used to inject specific instances. In Angular, the main parameter decorator is β€˜@Inject()β€˜, which is utilized in cases where you need to specify a custom provider for a dependency or inject a value outside the Angular DI system.

Example of an β€˜@Inject()β€˜ decorator:

import { Component, Inject } from '@angular/core';
import { MyService } from './my.service';
import { CUSTOM_VALUE } from './tokens';

@Component({
  selector: 'app-root',
  template: `<p>{{ value }}</p>`,
})
export class AppComponent {
  constructor(
    private myService: MyService,
    @Inject(CUSTOM_VALUE) public value: string
  ) {}
}

In conclusion, decorators in Angular are used to add metadata and modify the behavior of classes, properties, methods, and parameters. This allows the framework to understand their functionality and purpose within the application, and makes it easier to develop and maintain the code.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Angular interview β€” then scores it.
πŸ“ž Practice Angular β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Angular questions & answers β€” PDF + EPUB for $5

All 100 Angular questions Β· All topics