How to build a directive that conditionally adds elements to the DOM based on API responses

0 votes
Can Someone help me regarding How to build a directive that conditionally adds elements to the DOM based on API responses? please provide code and example.
2 days ago in HTML by Nidhi
• 14,600 points
23 views

1 answer to this question.

0 votes

Structural Directive with API Condition

1. Create the Directive

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

@Directive({

  selector: '[appShowIf]'

})

export class ShowIfDirective {

  constructor(

    private templateRef: TemplateRef<any>,

    private viewContainer: ViewContainerRef

  ) {}

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

    this.viewContainer.clear();

    if (condition) {

      this.viewContainer.createEmbeddedView(this.templateRef);

    }

  }

}


2. Usage in HTML with API Response

<div *appShowIf="apiSuccess">

  API returned success — show this content!

</div>


3. In Component.ts

apiSuccess = false;

ngOnInit() {

  this.http.get('https://api.example.com/data').subscribe({

    next: res => this.apiSuccess = true,

    error: err => this.apiSuccess = false

  });

}

answered 2 days ago by anonymous

Related Questions In HTML

0 votes
0 answers

How to GET background color of a particular portion on the html page

I used Gradient to set the color ...READ MORE

Jul 26, 2022 in HTML by Ashwini
• 5,430 points
1,429 views
0 votes
1 answer

How to align a <div> to the middle (horizontally/width) of the page?

Hello Kartik, position: absolute and then top:50% and left:50% places the top edge ...READ MORE

answered Apr 23, 2020 in HTML by Niroj
• 82,840 points
924 views
0 votes
0 answers

How to get the entire document HTML as a string?

Is there a way in javascript to ...READ MORE

Jun 3, 2022 in HTML by Tejashwini
• 3,820 points
557 views
0 votes
0 answers
0 votes
0 answers

how to create a HTML5 video element without it being shown in the page?

how is it possible to dynamically create ...READ MORE

Jul 5, 2022 in HTML by Tejashwini
• 3,820 points
658 views
0 votes
0 answers

What's the right way to decode a string that has special HTML entities in it?

Let's say I submit a service request ...READ MORE

Jul 8, 2022 in HTML by Tejashwini
• 3,820 points
583 views
0 votes
0 answers

How do Observables improve API call handling in Angular?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
106 views
0 votes
0 answers

What’s the difference between Observables and Promises?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
76 views
0 votes
0 answers

What type of operation do RxJS operators allow for observables?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
85 views
0 votes
0 answers

When to use switchMap vs concatMap?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
111 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