Search code examples
angularangular2-decorators

How angular's @Attribute decorator works?


I am new to learn Angular. I was learning about angular's decorators on angular.io. There is not much information about the @Attribute decorator. Please anyone give me some use cases.


Solution

  • The @Attribute decorator returns the value of the specified attribute from the host.

    For example:

    @Directive({
      selector: '[test]'
    })
    export class TestDirective {
      constructor(@Attribute('type') type ) {
        console.log(type); // text
      }
    }
    
    @Component({
      selector: 'my-app',
      template: `
        <input type="text" test>
      `,
    })
    export class App {}
    

    It useful for example when you don't need to use Inputs() and you don't want Angular to recheck the value in each change detection cycle. With Attribute you are getting the value once and your are done.