填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

Attribute

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

选项说明
attributeName

The name of the attribute whose value can be injected.

选项

The name of the attribute whose value can be injected.

attributeName: string
      
      attributeName: string
    

使用说明

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

<input type="text">
      
      <input type="text">
    

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

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

Example as TypeScript Decorator

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