How can structural directives manipulate the DOM in Angular

0 votes
With the help of code and example tell me How can structural directives manipulate the DOM in Angular?
Feb 26, 2025 in Angular by Nidhi
• 16,260 points
• 803 views

1 answer to this question.

0 votes

Structural directives in Angular (*ngIf, *ngFor, *ngSwitch) modify the DOM structure by controlling the presence of elements.

How They Work:

They use ViewContainerRef and TemplateRef to add or remove elements dynamically.

Example: Custom Directive (appExample)

import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core';

@Directive({

  selector: '[appExample]'

})

export class ExampleDirective {

  constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef) {}

  @Input() set appExample(condition: boolean) {

    condition ? this.viewContainer.createEmbeddedView(this.templateRef) : this.viewContainer.clear();

  }

}

answered Feb 26, 2025 by Navya

Related Questions In Angular

0 votes
1 answer

How to change the value of an Observable in TypeScript Angular?

To change the value of an Observable ...READ MORE

answered Feb 21, 2025 in Angular by Kavya
• 1,592 views
0 votes
1 answer
0 votes
1 answer

How can you log and inspect request and response headers in Angular?

In Angular, you can log request and ...READ MORE

answered Feb 25, 2025 in Angular by Navya
• 1,082 views
0 votes
0 answers

How can we combine multiple built-in pipes in Angular templates?

Can you help me with a code ...READ MORE

Mar 3, 2025 in Angular by Nidhi
• 16,260 points
• 983 views
+1 vote
8 answers

How can I implement process.env in Angular 5 environment?

Users do not have access to process.env ...READ MORE

answered Apr 3, 2018 in DevOps & Agile by DareDev
• 6,890 points
• 16,121 views
0 votes
1 answer
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
• 5,326 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
• 8,156 views
0 votes
1 answer

How can we add the style conditionally in Angular?

In Angular, you can add styles conditionally ...READ MORE

answered Feb 26, 2025 in Angular by Tanya
• 985 views
0 votes
1 answer

What is the use case of custom directives in Angular?

Custom directives are used to encapsulate reusable ...READ MORE

answered Feb 26, 2025 in Angular by Tanya
• 1,112 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP