Angular Directives
Angular directives are an essential part of the Angular framework, allowing you to extend the HTML vocabulary to build dynamic and interactive web applications. There are three main types of directives in Angular:
Components: Components are the most common type of directive in Angular. They are used to create custom HTML elements and encapsulate the behavior and presentation of a part of your application. Components have their own templates, styles, and logic.
Structural Directives: These directives change the structure of the DOM by adding or removing elements based on certain conditions. The most common structural directives are *ngIf, *ngFor, and *ngSwitch. They are prefixed with an asterisk (*) and are used to manipulate the structure of the DOM.
Attribute Directives: Attribute directives modify the behavior or appearance of an existing DOM element. Examples include ngClass, ngStyle, and custom attribute directives you create.
Let’s dive deeper into these directives:
1. Components
Components are the building blocks of an Angular application. They consist of three main parts: the component class, the HTML template, and the CSS styles.
Here’s a simple example of how to create a component:
1 2 3 4 5 6 7 8 9 10 11 |
// app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-root', // the custom HTML element name templateUrl: './app.component.html', // the HTML template file styleUrls: ['./app.component.css'] // optional CSS styles }) export class AppComponent { title = 'My Angular App'; } |
In this example, we’ve created an Angular component with the selector ‘app-root’, which means you can use
2. Structural Directives
Structural directives manipulate the DOM structure based on conditions. The most commonly used structural directives are:
*ngIf: It conditionally adds or removes elements from the DOM based on a boolean condition.
1 |
<div *ngIf="showElement">This element is shown if showElement is true.</div> |
*ngFor: It iterates over a collection (e.g., an array) and generates HTML for each item in the collection.
1 2 3 |
<ul> <li *ngFor="let item of items">{{ item }}</li> </ul> |
3. Attribute Directives
Attribute directives modify the behavior or appearance of an existing DOM element. Here’s an example of using the ngClass attribute directive:
1 |
<button ngclass="" active="" :="" isactive="" disabled="disabled" isdisabled="">Click me</button> |
In this example, the ngClass directive applies the active class if isActive is true and the disabled class if isDisabled is true.
Additionally, you can create your own custom attribute directives by implementing the Directive decorator and a corresponding directive class. These custom directives allow you to add specific behavior or styles to your HTML elements.
This is a basic introduction to Angular directives. As you delve deeper into Angular development, you’ll discover more advanced features and techniques for building powerful web applications.
0 Comments