Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

Attribute

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

OptionDescription
attributeName

The name of the attribute whose value can be injected.

Options

The name of the attribute whose value can be injected.

      
      attributeName: string
    

Usage notes

Suppose we have an <input> element and want to know its type.

      
      <input type="text">
    

The following example uses the decorator to inject the string literal text in a directive.

      
      @Directive({selector: 'input'})
class InputAttrDirective {
  constructor(@Attribute('type') type: string) {
    // type would be 'text' in this example
  }
}
    

The following example uses the decorator in a component constructor.

      
      @Component({selector: 'page', template: 'Title: {{title}}'})
class Page {
  title: string;
  constructor(@Attribute('title') title: string) {
    this.title = title;
  }
}