What is Directives ?
- Get link
- X
- Other Apps
Directives
Directives ka matlab hai instructions ya commands jo kisi program, compiler, ya system ko batati hain ki kaise kaam karna hai. Ye mostly programming aur web development me use hote hain.
Thoda simple example me:
-
Programming me:
-
C/C++ me
#include <stdio.h>ek directive hai jo compiler ko batata hai ki standard input-output library ko include karo. -
Iska kaam hota hai program me functions like
printf()use karna.
-
-
Web/Frameworks me:
-
Angular me
*ngIfya*ngFordirectives hain, jo HTML elements ke behavior ko control karte hain. -
Example:
*ngIf="isLoggedIn"→ ye element tabhi dikhaye jabisLoggedIntrue ho.
-
Summary:
Directive = ek instruction ya rule jo compiler, program, ya framework ko control karne ke liye diya jata hai.
1. Programming Directives (C/C++ Example)
Ye mostly compiler ko instructions dene ke liye use hote hain.
Types:
-
Include Directives – kisi library ko program me include karne ke
-
Example:
#include <stdio.h>→ Input/Output functions ke liye.
-
-
Define Directives – constants ya macros define karne ke liye.
-
Example:
#define PI 3.14→ PI ko 3.14 se replace kar do.
-
-
Conditional Directives – code ko condition ke basis pe compile karne ke liye.
-
Example:
#ifdef DEBUG printf("Debugging info"); #endif
Ye sirf tab compile hoga jab DEBUG define ho.
-
2. Web Framework Directives (Angular Example)
Ye HTML elements ke behavior ya appearance ko control karte hain.
Types:
-
Structural Directives – DOM structure change karte hain (elements ko add/remove karna).
-
Example:
*ngIf,*ngFor
<div *ngIf="isLoggedIn">Welcome!</div>→ Ye div tabhi dikhayega jab
isLoggedIn = trueho. -
-
Attribute Directives – existing elements ke look ya behavior ko modify karte hain.
-
Example:
ngStyle,ngClass
<p [ngStyle]="{'color': textColor}">Hello</p>→ Ye paragraph ka color dynamically change karega.
-
1. Programming Directives (C/C++ example)
Purpose: Compiler ko instructions dena ki code ko kaise process karna hai.
| Directive Type | Example | Kaam (Purpose) |
|---|---|---|
| #include | #include <stdio.h> | Library include karna, jaise input/output functions. |
| #define | #define PI 3.14 | Constants ya macros define karna. |
| #ifdef / #ifndef | #ifdef DEBUG ... #endif | Conditional compilation, code compile hoga sirf condition pe. |
| #pragma | #pragma once | Compiler ko special instructions dena. |
| #undef | #undef PI | Pehle define kiye macro ko remove karna. |
Example in code:
#include <stdio.h>
#define PI 3.14
int main() {
printf("Value of PI: %f", PI);
return 0;
}
2. Angular / Web Directives
Purpose: HTML elements ke behavior aur appearance ko control karna.
| Directive Type | Example | Kaam (Purpose) |
|---|---|---|
| Structural | *ngIf="isLoggedIn" | Element show/hide karna condition ke basis pe. |
| Structural | *ngFor="let item of items" | List ke elements dynamically render karna. |
| Attribute | [ngStyle]="{'color':'red'}" | Element ka style change karna. |
| Attribute | [ngClass]="{'active': isActive}" | Element ka class dynamically change karna. |
Example in HTML (Angular):
<ul>
<li *ngFor="let fruit of fruits">{{ fruit }}</li>
</ul>
<p *ngIf="isLoggedIn">Welcome back!</p>
💡 Quick Tip:
-
Programming me directives mostly compile time instructions hote hain.
-
Angular/Web me directives mostly runtime behavior control karte hain.
can make mistakes
- Get link
- X
- Other Apps
Comments
Post a Comment