PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Angular · Theory

Directives

← All stacks

Theory

8/43

Directives

A directive changes an existing element. Attribute directive: highlight Fail rows. Structural: *ngIf / *ngFor add or remove nodes. Components are directives with a template.

Write @Directive({ selector: '[appFail]' }) and HostBinding('class.fail'). Use it as <tr appFail>. Don’t build a whole component just to add a class.

Trap — putting business rules inside a directive that should live in a service. Directives are for the DOM.

Exam tip

Attribute vs structural. One HostBinding example.

Example

// template
// <p *ngIf="ok">Visible</p>
// <li *ngFor="let u of users">{{ u }}</li>

users = ["Asha", "Ravi"];
ok = true;

Directives — *ngIf toggles DOM; *ngFor repeats a template per item.

Short notes

  • DefChange an element — attribute or structural.
  • RuleDOM job → directive. Data job → service.
  • TrapFat directive with API calls.

Questions

1

Directive vs component?

2

Name two structural directives.

Previous← TemplatesNextData Binding →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.