What is a directive? What are the types?
Model answer
A directive adds behaviour to an element in a template. There are three kinds. Components are directives with a template. Structural directives change the DOM structure by adding or removing elements, such as the older *ngIf and *ngFor. Attribute directives change how an element looks or behaves, such as ngClass, ngStyle or your own custom directive.
You create a custom one with the @Directive decorator and a selector, for example to highlight an element when the mouse is over it.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is the difference between ngClass and ngStyle, and how do they compare with class and style bindings?
- What is a pipe? Name some built-in pipes.
- How do you create a custom pipe? What is the difference between pure and impure pipes?
- What does the async pipe do, and why is it recommended?
- How do you build a custom attribute directive?
- What is the difference between providedIn root and providing a service in a component?