What is @Attribute decorator in Angular?

919    Asked by RoseKnox in Python , Asked on Jan 21, 2020
Answered by Rose Knox

The @Attribute() decorator is a special feature used only by the AOT compiler. When the template is compiled into TypeScript the factory function for the component (from the perspective of the parent template) passes the attribute value to the component constructor. There has to be a parent DOM element that contains the DOM element for the new child component, and the attributes are read from that element.

The code written by the AOT is different from the component factories you use to dynamically create components, and since the dependency injector is by-passed for @Attribute() values and there is no data binding. The execution time for using attributes in the constructor is much faster. I think that's something people often overlook and it's good to know.

The only thing you can do is support both attributes and dependency injector in the component, and use the value from either in the constructor.

Your Answer


Parent Categories