Attribute Directives: Jogesh K. Muppala
Attribute Directives: Jogesh K. Muppala
Directives
Jogesh K. Muppala
Directives
• Angular templates are dynamic
• Directives give instructions to Angular on how to render the
templates to the DOM
• A directive can be defined in Angular as a class with the @Directive
decorator
• A component is a special kind of directive with a template
associated to it
• Two other kinds of directives in Angular: Structural and Attribute
2
Attribute Directives
• Used as attributes for elements in the template
• Listen to and modify the behavior of other HTML
elements, attributes, properties, and components
• Built-in attribute directives: NgStyle, NgModel, NgClass
• Some of the other Angular modules like the
FormsModule and RouterModule define their own
attribute directives
3
Custom Directives
• Can create our own custom directives
• Directive imported from @angular/core
– @Directive decorator
• ElementRef also imported from @angular/core
– Injected into the directive’s constructor so that the
code can access the DOM element