Angular Directives
Angular Directives
Contents
• What is Angular Directive?
• Types of Directives
• Attribute Directives
• Structural Directives
What is Angular Directive?
• Allows to enhance the behaviour of elements within Angular App
• classes that add additonal behaviour to elements
• built-in and custom directives
Types of Angular Directive?
• Attribute Directives - Change the appearance of elements, components or other directives
• Structural Directives - Change the DOM layout by adding and removing DOM elements
Attribute Directives
• Attribute directives are special directives that can modify the behaviour of HTML
elements, attributes, properties or Components
• They do listen to certain events or conditions and make the changes accordingly
• Built-In Attribute Directives are:
ngClass - add or remove CSS classes
ngStyle - add or remove CSS styles
ngModel - add two-way-data binding
Dynamic Styling with ngClass
• In home.component.ts file, define a properties as below:
export class HomeComponent
{
isSuccess = false;
h2CSSProps = "text-danger";
successColor = "text-success";
errorColor = "text-danger";
}
• ngFor - repeat specific or group of HTML elements for each item in list or array
intVal = 10;
<div [ngSwitch]='intVal'>
<h2 *ngSwitchCase = '0'> Value = {{ intVal }}</h2>
<h2 *ngSwitchCase = '10'> Value = {{ intVal }}</h2>
<h2 *ngSwitchCase = '20'> Value = {{ intVal }}</h2>
<h2 *ngSwitchDefault> Default value</h2>
</div>
?