Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 637
mia Avatar asked Jan 01 '18 07:01

mia


1 Answers

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.

like image 89
Bazinga Avatar answered Sep 28 '22 11:09

Bazinga