Back to blog
Mar 07, 2025
3 min read

Angular Decorator @Attribute

Parameter decorator for a directive constructor that designates a host-element attribute whose value is injected as a constant string literal.

The @Attribute decorator in Angular is used to inject the value of a host element attribute into a directive or component constructor. This is particularly useful when you need to access or manipulate the attributes of the host element, or when you want to customize the behavior of a directive or component based on the value of a specific attribute.

For example, suppose you are building a custom form input component that should support various input types, such as text, email, number, and so on. You can use the @Attribute decorator to inject the value of the type attribute of the host <input> element into the component constructor, and then use that value to customize the behavior of the component accordingly.

Here’s an example of how to use the @Attribute decorator in a custom form input component:

import { Component, Input, Attribute } from '@angular/core';

@Component({
  selector: 'app-input',
  template: `
    <input type="{{type}}" [value]="value">
  `,
})
export class AppInputComponent {
  @Input() value: string;
  @Attribute('type') type: string;
}

In this example, the @Attribute('type') type: string line uses the @Attribute decorator to inject the value of the type attribute of the host <input> element into the type property of the component. The component template then uses the type property to set the type attribute of the inner <input> element.

When you use this component in your application, you can pass the desired input type as an attribute to the component:

<app-input type="email" [value]="email"></app-input>

This will render an <input> element with the type attribute set to email, and the component will use that value to customize its behavior accordingly.

Note that the @Attribute decorator can only be used in directives and components, and it can only inject string values. If you need to inject other types of values, such as numbers or objects, you can use other dependency injection mechanisms provided by Angular.

Key points about the @Attribute decorator in Angular:

  • The @Attribute decorator is used to inject the value of a host element attribute into a directive or component constructor.
  • The @Attribute decorator takes a string argument that specifies the name of the attribute to inject.
  • The @Attribute decorator can only be used in directives and components.
  • The @Attribute decorator can only inject string values.
  • To use the @Attribute decorator, you need to declare a property in the directive or component class with the same name as the attribute, and decorate it with the @Attribute decorator.
  • The injected attribute value can be used to customize the behavior of the directive or component.
  • The @Attribute decorator is particularly useful when building custom form input components, where you may need to access or manipulate the attributes of the host element.