There are three types of directives present in Angular.
- Components are directives with a template or view.
- @Component decorator is actually @Directive with templates .
Structural Directives
A Structural directive changes the DOM layout by adding and removing DOM elements.
*directive-name = expression
Built-in structural directives,
ngIf directive renders components or elements conditionally based on whether or not an expression is true or false. ngIf directive removes the element from the DOM tree.
Syntax
*ngIf = "expression"
ngfor
ngFor directive is used to iterate over collection of data i.e., arrays.
Syntax
*ngFor = "expression"
ngswitch
With NgSwitch we evaluate the expression only once and then choose the element to display based on the outcome.
Attribute Directives
Attribute directives changes the appearance / behavior of a component / element. Below are the built in attribute directives,
This directive is used to modify a component/element’s style. We can use the following syntax to set a single CSS style to the element which is also known as style binding.
Syntax
[style.<cssproperty>] = "value"
ngclass
It allows us to dynamically set and change the CSS classes for a given DOM element. We can use the following syntax to set single css class to the element which is also known as class binding.
Syntax
[class.<css_class_name>] = "property/value"
Observation
Here, we have discussed about the types of directives and the syntax, so in the next article- we are going to explore the concept with the help of examples.
Summary
In this article, we explored how Angular detects the changes and updates it in the application at respective places. Also we discussed about directives in Angular. Hope you like the article. Until next time- Happy Reading
Cheers