W3cubDocs

/Angular 12

AttributeDecorator

interface

Type of the Attribute decorator / constructor function.

interface AttributeDecorator {
  (name: string): any
  new (name: string): Attribute
}

Methods

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

(name: string): any

Parameters
name string
Returns

any

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;
  }
}

new (name: string): Attribute

Parameters
name string
Returns

Attribute

© 2010–2021 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v12.angular.io/api/core/AttributeDecorator