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 in Angular by Nidhi
• 10,860 points
40 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 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 in Angular by Kavya
117 views
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 in Angular by Navya
50 views
0 votes
0 answers

What is the use case of custom directives in Angular?

With the help of code and example ...READ MORE

Feb 26 in Angular by Nidhi
• 10,860 points
32 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
13,452 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
2,045 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
3,940 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 in Angular by Tanya
50 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 in Angular by Tanya
58 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