🔷 What is a Directive in Angular?
A directive is a special instruction in Angular that changes the behavior, appearance, or structure of DOM elements.
👉 In simple words:
Directives teach HTML new tricks.
🧩 Types of Directives in Angular
Angular provides three types of directives:
Structural Directives
Attribute Directives
🧱 1️⃣ Structural Directives (Change DOM Structure)
📌 Structural directives add, remove, or manipulate elements in the DOM.
👉 They are identified by * (asterisk)
🔹 Why * is used?
The * tells Angular:
“This directive will change the structure of the DOM.”
Internally, Angular converts it into <ng-template>.
✅ Common Structural Directives
Conditionally adds/removes elements.
✔ True → element exists
❌ False → element removed from DOM
🔸 *ngFor
Used to loop through collections.
🔸 *ngSwitch
Used for multiple conditions.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
// Used for ngIf
isLoggedIn: boolean = false;
// Used for ngFor
students: string[] = ['Rahul', 'Amit', 'Neha', 'Pooja'];
toggleLogin() {
this.isLoggedIn = !this.isLoggedIn;
}
}
📄 Template File – app.component.html
🔹 Example 1: *ngIf (Condition Based Rendering)
<h2>*ngIf Example</h2>
<button (click)="toggleLogin()">
Toggle Login
</button>
<p *ngIf="isLoggedIn">
Welcome! You are logged in.
</p>
<p *ngIf="!isLoggedIn">
Please login first.
</p>
<hr />
🔹 Example 2:
*ngFor (Loop / List Rendering)
<h2>*ngFor Example</h2>
<ul>
<li *ngFor="let student of students; index as i">
{{ i + 1 }}. {{ student }}
</li>
</ul>
<hr />
app.component.html🔹 Role Selection Buttons
🧠 Structural Directive Summary
| Directive | Purpose |
|---|---|
*ngIf | Conditional rendering |
*ngFor | Loop rendering |
*ngSwitch | Multiple conditions |
🎨 2️⃣ Attribute Directives (Change Appearance/Behavior)
📌 Attribute directives do not remove elements
They modify properties, styles, or behavior of elements.
👉 Used without *
✅ Common Attribute Directives
🔹 ngClass
Dynamically add/remove CSS classes.
.css
p {
padding: 10px;
font-weight: bold;
border: 1px solid #ccc;
width: 200px;
text-align: center;
}
/* Active Style */
.active {
background-color: lightgreen;
color: green;
}
/* Error Style */
.error {
background-color: #ffdddd;
color: red;
}
.ts
isActive:boolean = true;
hasError:boolean=false
.html
🔹 ngStyle
Apply dynamic inline styles.
.ts file
role: string = 'guest';
changeRole(s:string)
{
this.role=s
}
.html file
<button (click)="changeRole('admin')" [ngStyle]="{'background-color':role==='admin'?'yellow':'red'}">Admin</button>
<button (click)="changeRole('user')">User</button>
<button (click)="changeRole('guest')">Guest</button>
🔹 [disabled], [hidden]
Control element properties.
🧠 Attribute Directive Summary
| Directive | Purpose |
|---|---|
ngClass | CSS class binding |
ngStyle | Inline style binding |
[hidden] | Show/Hide (CSS) |
⚠️ *ngIf vs [hidden] (Interview Favorite)
| Feature | *ngIf | [hidden] |
|---|---|---|
| DOM Removal | ✅ Yes | ❌ No |
| Performance | Better | Less |
| Visibility | Element destroyed | CSS hidden |

1 Comments
I have created calculator app in js but i can not know how to convert js code in ts codes this is the code function Solve(val) {
ReplyDeletevar v = document.getElementById('result');
v.value += val;
}
function Result() {
var num1 = document.getElementById('result').value;
try {
var num2 = eval(num1.replace('x', '*'));
document.getElementById('result').value = num2;
} catch {
document.getElementById('result').value = 'Error';
}
}
function Clear() {
var inp = document.getElementById('result');
inp.value = '';
}
function Back() {
var ev = document.getElementById('result');
ev.value = ev.value.slice(0, -1);
}
document.addEventListener('keydown', function (event) {
const key = event.key;
const validKeys = '0123456789+-*/.%';
if (validKeys.includes(key)) {
Solve(key === '*' ? 'x' : key);
} else if (key === 'Enter') {
Result();
} else if (key === 'Backspace') {
Back();
} else if (key.toLowerCase() === 'c') {
Clear();
}
}); please tell me for this
POST Answer of Questions and ASK to Doubt