Angular Directives
Directives existing elements மற்றும் components-க்கு behavior add செய்கின்றன.
Directive Essentials
- உறுப்புகளுக்கு நடத்தை சேர்க்க: @Directive மற்றும் ஒரு selector உடன் elements-க்கு behavior add செய்யவும்.
- கட்டமைப்பு directives: Structural directives (*ngIf, *ngFor) DOM-ஐ add/remove செய்கின்றன.
- பண்புக்கூறு directives: Attribute directives ([ngClass], custom [w3X]) nodes create/remove செய்யாமல் look/behavior change செய்கின்றன.
- நட்சத்திர தொடரியல்: Star syntax (*) sugar ஆகும், அது <ng-template>-க்கு expand செய்கிறது.
- Directive inputs-ஐ வெளிப்படுத்த: @Input() உடன் directive inputs expose செய்யவும்; @Input('alias') உடன் alias செய்யவும் [alias] மூலம் bind செய்ய.
Directive Examples
*ngIf="condition"
*ngFor="let item of items"
@Directive({ selector: '[w3Highlight]' })
<div w3Highlight></div>
Notes
- தொடர்புடையவை: Templates, Data Binding, Conditional Rendering, மற்றும் Lists ஆகியவற்றை காணவும்.
Basic Directives
*ngIf ஒரு நிபந்தனையின் அடிப்படையில் உள்ளடக்கத்தை காட்டுகிறது/மறைக்கிறது.
*ngFor ஒவ்வொரு பட்டியல் உருப்படிக்கும் ஒரு தொகுதியை மீண்டும் செய்கிறது.
பட்டியலை add/remove செய்ய ஒரு கொடியை toggle செய்யவும்; உருப்படிகளை *ngFor உடன் render செய்யவும்.
Basic Directives Syntax
<p *ngIf="items.length > 0">We have {{ items.length }} items</p>
<li *ngFor="let item of items">{{ item }}</li>
Example
Basic Directives Example
import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule],
template: `
<h3>Directives</h3>
<p *ngIf="items.length > 0">We have {{ items.length }} items</p>
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>
<button (click)="toggle()">Toggle Items</button>
`
})
export class App {
show = true;
get items() { return this.show ? ['Angular', 'Components', 'Directives'] : []; }
toggle() { this.show = !this.show; }
}
bootstrapApplication(App);
Example Explained
- *ngIf: நிபந்தனை உண்மை/பொய் ஆக இருக்கும் போது பத்தியை DOM-இலிருந்து add/remove செய்கிறது.
- *ngFor: Items-ல் உள்ள ஒவ்வொரு உருப்படிக்கும் <li> தொகுதியை repeat செய்கிறது.
- Toggle: பொத்தான் show-ஐ flip செய்கிறது, கணக்கிடப்பட்ட items getter-ஐ மாற்றுகிறது.
Notes
- CommonModule தேவை: Standalone components-ல் built-in directives பயன்படுத்தும் போது CommonModule import செய்யவும்.
- நட்சத்திர தொடரியல் sugar ஆகும்: *ngIf/*ngFor <ng-template>-க்கு expand செய்கின்றன; DOM nodes உண்மையில் தோன்றும்/மறையும்.
- வார்ப்புருக்களில் கனரக வேலையைத் தவிர்க்கவும்: *ngFor-ல் விலையுயர்ந்த செயல்பாடுகளை call செய்யாதீர்கள். Component-ல் compute செய்யவும். நீண்ட பட்டியல்களுக்கு, Lists பார்க்கவும் மற்றும் trackBy பயன்படுத்தவும்.
- ஒரு கட்டமைப்பு directive ஒரு host-க்கு: ஒரே உறுப்பில் இரண்டு * directives வைக்காதீர்கள். தேவைப்பட்டால் ஒன்றை <ng-container>-ல் wrap செய்யவும்.
ngIf with else
Else உடன் *ngIf-ஐ ஒரு fallback வார்ப்புருவை சுட்டிக்காட்டவும்.
இரண்டு கிளைகளையும் வெளிப்படையாக உருவாக்க then/else தொடரியலைப் பயன்படுத்தவும்.
Angular DOM தொகுதிகளை முழுவதுமாக add/remove செய்கிறது.
ngIf with else Syntax
<ng-container *ngIf="loggedIn; else loggedOut"></ng-container>
<ng-template #loggedOut>...</ng-template>
<ng-container *ngIf="hasAccess; then accessTpl; else noAccessTpl"></ng-container>
Example
ngIf with else Example
import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule],
template: `
<h3>ngIf with else</h3>
<button (click)="loggedIn = !loggedIn">
{{ loggedIn ? 'Log out' : 'Log in' }}
</button>
<ng-container *ngIf="loggedIn; else loggedOut">
<p>Welcome back, {{ user }}!</p>
</ng-container>
<ng-template #loggedOut>
<p>Please log in to continue.</p>
</ng-template>
<hr>
<h4>ngIf then/else syntax</h4>
<button (click)="hasAccess = !hasAccess">
Toggle Access ({{ hasAccess ? 'granted' : 'denied' }})
</button>
<ng-container *ngIf="hasAccess; then accessTpl; else noAccessTpl"></ng-container>
<ng-template #accessTpl>
<p style="color: seagreen">Access granted.</p>
</ng-template>
<ng-template #noAccessTpl>
<p style="color: crimson">Access denied.</p>
</ng-template>
`
})
export class App {
loggedIn = false;
user = 'Angular User';
hasAccess = true;
}
bootstrapApplication(App);
Example Explained
- *ngIf ... else: நிபந்தனை உண்மையாக இருக்கும் போது முக்கிய தொகுதியை render செய்கிறது; இல்லையெனில் #loggedOut ஆல் குறிப்பிடப்படும் வார்ப்புருவை render செய்கிறது.
- then/else: Then/else வடிவம் பெயரிடப்பட்ட வார்ப்புருக்கள் மூலம் இரண்டு கிளைகளையும் வெளிப்படையாக்குகிறது.
- State: பொத்தான்கள் loggedIn மற்றும் hasAccess-ஐ toggle செய்கின்றன, இரண்டு கிளைகளையும் demonstrate செய்ய.
Notes
- வார்ப்புரு refs: Else/then-க்கு நீங்கள் pass செய்யும் #ref ஒரு <ng-template>-ஐ குறிப்பிடுகிறது. இந்த குறியீடு rendered ஆகும் வரை DOM-ல் இருக்காது என்பதை நினைவில் கொள்ளவும்.
- கூடுதல் DOM-ஐத் தவிர்க்கவும்: கூடுதல் உறுப்புகளைச் சேர்க்காமல் கட்டமைப்பைக் குழுவாக்க <ng-container> பயன்படுத்தவும்.
Attribute Directive (hover highlight)
ஒரு இருக்கும் உறுப்பில் இயங்குகிறது (DOM created/destroyed இல்லை).
தோற்றம் அல்லது நடத்தையை மாற்றுகிறது (எ.கா., styles, classes, attributes add செய்கிறது).
எடுத்துக்காட்டு hover-ல் background set செய்ய @HostBinding மற்றும் @HostListener பயன்படுத்துகிறது.
Attribute Directive Syntax
@Directive({ selector: '[w3Highlight]' })
<div [w3Highlight]="'lightyellow'">Hover me</div>
Example
Attribute Directive Example
import { bootstrapApplication } from '@angular/platform-browser';
import { Component, Directive, Input, HostBinding, HostListener } from '@angular/core';
import { CommonModule } from '@angular/common';
@Directive({
selector: '[w3Highlight]',
standalone: true
})
export class HighlightDirective {
@Input('w3Highlight') highlightColor = 'lightyellow';
@HostBinding('style.transition') transition = 'background-color 150ms ease-in-out';
@HostBinding('style.backgroundColor') bg = '';
@HostListener('mouseenter') onEnter() { this.bg = this.highlightColor; }
@HostListener('mouseleave') onLeave() { this.bg = ''; }
}
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, HighlightDirective],
styles: [`
.box { padding: 10px; border: 1px dashed #bbb; border-radius: 6px; }
`],
template: `
<h3>Attribute Directive (highlight)</h3>
<p>Hover the first box to see the effect:</p>
<div class="box" [w3Highlight]="'lightyellow'">I get highlighted on hover</div>
<div class="box" style="margin-top:8px">I do not</div>
`
})
export class App {}
bootstrapApplication(App);
Example Explained
- [w3Highlight]="'lightyellow'": அதன் input alias மூலம் directive-க்கு நிறத்தை pass செய்கிறது.
- @HostBinding: உறுப்பின் style.backgroundColor மற்றும் transition பண்புகளை directive fields-லிருந்து bind செய்கிறது.
- @HostListener: பின்னணி நிறத்தை set/clear செய்ய mouseenter/mouseleave-க்கு react செய்கிறது.
Notes
- Selector மோதல்கள்: Custom directives-க்கு குறிப்பிட்ட selectors பயன்படுத்தவும் (எ.கா., [w3Highlight]) மற்ற நூலகங்களுடன் மோதுவதைத் தவிர்க்க.
- செயல்திறன்: Host listeners லேசானதாக வைக்கவும்; @HostListener handlers-ல் கனரக ஒத்திசைவு வேலையைத் தவிர்க்கவும்.