Adding a component dynamically to a div in Angular 5

Can anyone help me with this issue?

I have successfully managed to dynamically append an element, but it is not getting compiled. I have looked at several tutorials like this one

However, these tutorials do not exactly meet my requirements. They often use hashtag notation to identify the container.

What I need is to append a component to any element that might have my custom directive on it.

In addition, I would like to utilize the bound value of the directive to control a hidden attribute on the appended element.

My Objectives:

  1. Customize existing component behavior:
    • Add an attribute for show/hide functionality
    • Add a class for styling customization
  2. Simplify HTML coding
    • Eliminate the need to write the entire component <my-comp></mycomp>
    • Avoid specifying the class explicitly
    • Automatic functionality if the class name changes
      1. Modify the element where the directive is applied
    • Ultimately, add a class to the container element

Expected Input Source:

<div [myDirective]="myBoolean">
    <p>some content</p>
</div>

Expected Output After Compilation:

<div [myDirective]="myBoolean" class="myDirectiveClass1">
    <p>some content</p>
     <someComponent [hidden]="myBoolean" class="myDirectiveClass2"></someComponent>
</div>

Is there a solution to achieve this? Any help would be greatly appreciated.

Answer №1

The concept is quite straightforward. I have provided an example for better understanding.

Take a moment to carefully go through the comments within the loader directive.

Visit this link for further details

UPDATE:

Your component:

export class LoaderComponent {

  loading;

  constructor() { }

}

Your directive

export class LoaderDirective implements OnDestroy {

  private componentInstance: ComponentRef<LoaderComponent> = null;

  @Input()
  set appLoader(loading: boolean) {
    this.toggleLoader(loading);
  }

  constructor(
    private viewContainerRef: ViewContainerRef,
    private componentFactoryResolver: ComponentFactoryResolver
  ) { }

  toggleLoader(loading: boolean) {
    if (!this.componentInstance) {
      this.createLoaderComponent();
      this.makeComponentAChild();
    }

    this.componentInstance.instance.loading = loading;
  }

  private createLoaderComponent() {
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(LoaderComponent);
    this.componentInstance = this.viewContainerRef.createComponent(componentFactory);
  }

  private makeComponentAChild(){
    const loaderComponentElement = this.componentInstance.location.nativeElement;
    const sibling: HTMLElement = loaderComponentElement.previousSibling;
    sibling.insertBefore(loaderComponentElement, sibling.firstChild);
  }

  ngOnDestroy(): void {
    if (this.componentInstance) {
      this.componentInstance.destroy();
    }
  }

}

Your module

@NgModule({
  ...
  entryComponents: [
    LoaderComponent
  ]
})

Answer №2

Here is the method I used to make it functional

import {
  Renderer2,
  Directive,
  Input,
  ElementRef,
  OnChanges,
  ViewEncapsulation
} from "@angular/core";
import { MatSpinner } from "@angular/material";

@Directive({
  selector: "[myDirective]"
})
export class MyDirective {

  @Input()
  set myDirective(newValue: boolean) {
    console.info("myDirectiveBind", newValue);
    if (!!this._$matCard) {
      const method = newValue ? "removeClass" : "addClass";
      this.renderer[method](this._$matCard, "ng-hide");
    }
    this._myDirective = newValue;
  }

  private _myDirective: boolean;
  private _$matCard;

  constructor(private targetEl: ElementRef, private renderer: Renderer2) {
    this._$matCard = this.renderer.createElement('mat-card');
    const matCardInner = this.renderer.createText('Dynamic card!');
    this.renderer.addClass(this._$matCard, "mat-card");
    this.renderer.appendChild(this._$matCard, matCardInner);
    const container = this.targetEl.nativeElement;
    this.renderer.appendChild(container, this._$matCard);
  }


}

import {
  Component,
  ElementRef,
  AfterViewInit,
  ViewEncapsulation
} from '@angular/core';

@Component({
  selector: 'card-overview-example',
  templateUrl: 'card-overview-example.html',
  styleUrls: ['card-overview-example.css']
})
export class CardOverviewExample {
  
  hideMyDirective = !1;

  constructor(private _elementRef: ElementRef) { }

  getElementRef() {
    return this._elementRef;
  }

  ngAfterViewInit() {
    let element = this._elementRef.nativeElement;
    let parent = element.parentNode;
    element.parentNode.className += " pippo";

  }
}
.ng-hide {
  display: none;
}
<mat-card>Simple card</mat-card>
<div class="text-center">
  <button (click)="hideMyDirective = !hideMyDirective">
    Toggle show dynamic card
</button>
</div>
<br />
<span>hideMyDirective: {{hideMyDirective}}</span>
<hr />
<div class="myDiv" [myDirective]="hideMyDirective">
    <ul>
      <li>My content</li>
      </ul>
</div>

Answer №3

Within the designated component's HTML file:

<div #target>
</div>

In the TypeScript file of the intended component:

'Component_to_insert' refers to the component that will be inserted into another component.

import { Component_to_insert } from 'path';
import { Component, ViewChild, ViewContainerRef, ComponentFactoryResolver, AfterViewInit } from '@angular/core';

@Component({
selector: 'component-name',
templateUrl: 'component.html',
styleUrls: ['component.scss'],
entryComponents: [Component_to_insert]
})

export class ManagetemplatesPanelComponent implements AfterViewInit {

    @ViewChild('target', { read: ViewContainerRef }) entry: ViewContainerRef;

    constructor(private resolver: ComponentFactoryResolver) { }

    ngAfterViewInit() {
     this.createComponent();
    }

    createComponent() {
      this.entry.clear();
      const factory = this.resolver.resolveComponentFactory(Component_to_insert);
      const componentRef = this.entry.createComponent(factory);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular mat-icon can render intricate pictures

Embarking on my journey with Angular, I have recently delved into the world of mat icons. While I have managed to create simple mat icons successfully, I find myself stumped when attempting to tackle more intricate designs. Take for instance the image belo ...

What is the best way to retrieve the most recent entry in a Firebase real-time database?

Utilizing Firebase's real-time database, I am updating values in a chart as they change. However, my struggle lies in retrieving only the most recent value added to the database. While browsing through limitToLast and 'child_added' do not w ...

What is the best way to update properties in a child component using React hooks?

Looking to update the props using react hooks, I came across a method of passing setState function as props to the child component. MainContainer.tsx const MainContainer: React.FC = () => { const [count, setCount] = useState(0); return <Counter ...

How can the value of a number in Angular be changed without altering its original value?

Imagine having the initial number 100. If I enter 50 in another input, it should add 50 to 100. However, if I then change the value from 50 to 80, the total should be 180 and not 230. The goal is always to add numbers to the original sum, not the new valu ...

Parsing of the module failed due to an unexpected character appearing when trying to insert a TTF file into the stylesheet

As a newcomer to Angular, I recently completed the tutorial and am now working on my first app. While trying to add an OTF file, everything went smoothly. However, when I made a change to my app.component.css file and included this code snippet: @font-fa ...

Creating a unique custom pipe in Angular 5

Recently, I started learning Angular and encountered an issue that I'm struggling with. I am trying to develop a custom pipe that can convert decimal codes into base64 images and then display them in views. Even though I have the complete code to add ...

Exploring the Synergy of Nestjs Dependency Injection with Domain-Driven Design and Clean Architecture

I am currently exploring Nestjs and experimenting with implementing a clean-architecture structure. I would appreciate validation of my approach as I am unsure if it is the most effective way to do so. Please note that the example provided is somewhat pseu ...

Removing data from MongoDB with AngularLet's explore how to delete

I am having trouble removing a record from MongoDB using Angular on the FrontEnd. I have tried several methods but none seem to be working. This is my Express Delete request: router.get('/ideas/:id', function(req, res){ ideas_data.remove({_id ...

What is the process for integrating the node-menu package into my project without utilizing the require statement?

Is there a way to incorporate node-menu into my TypeScript project without using require, like this: const menu = require('node-menu'); Whenever I attempt to import node-menu into my project, I encounter the following errors: https://i.sstatic. ...

Extract particular "set" from JSON retrieval

For instance, here is a sample output, but the JSON response will be much longer. I am only interested in filtering out NFTs with a specific contract address (collection) from this response. Currently utilizing axios and typescript in React. { "own ...

What is the proper type for an object and an array of strings?

We have an array example below. const sampleArray = [ {names: ['example1', 'example2']}, 'another', 'final' ]; Additionally, here is a type error example. The error message reads as follows: "Type 'string ...

What steps can I take to ensure the footer remains fixed at the bottom of the webpage?

I am facing a challenge with my footer component as it is currently being displayed at the end of another component. I want to ensure that it remains anchored at the bottom of the page regardless of the other components present. In addition to the footer ...

The date selected in the date picker does not display in the view until the calendar is opened

I am currently working with two date pickers named startDate and endDate. My goal is to set up a basic date validation system where the start date cannot come after the end date. The main requirement is that if the user selects an end date that is earlier ...

Positioning gallery images in Angular Modal Gallery

I'm currently working on implementing an image modal, but I've run into several issues. My goal is to have the first image fill the entire large box (class drop), with the rest displayed below it, as illustrated in the image. I've experime ...

Navigating within components using code is an essential skill when working with Vue Router

I am currently developing a Quasar application powered by Vue 3 with vue-router version 4 All my routes are properly configured and function well when navigating from a component template using <router-link to="/route">Go to route</rout ...

When using the Google API load callback, the Angular(4) router does not actually replace routes; instead, it stacks them up each time

I've been attempting to implement Google Authentication in my Angular 4 application. I have successfully loaded the Google platform.js and api.js in my index.html file. When I click on the login button, this is what I have coded: gapi.load('auth ...

experimenting with a TypeScript annotation

I have created a simple decorator that can trigger either stopPropagation() or preventDefault() based on certain conditions. I have thoroughly tested this decorator in my application and am confident that it works correctly. However, I encountered an issue ...

An issue has been identified with the functionality of the router-out

Issue with Router Loading Component Outside of the router-outlet in app.component.ts @Component({ selector : "body", template : `<router-outlet></router-outlet>`, directives: [ROUTER_DIRECTIVES] }) @RouteConfig([ {path: "/aut ...

Can you explain the meaning of the TypeScript code snippet below?

Recently, while going through the declaration file of reflect-metadata library, I came across the following code snippet. It appears that the function metadata is expected to return an object with 2 functions. Could someone kindly explain the purpose of ...

Enhance the efficiency of writing the *.d.ts file

I have recently started diving into TypeScript with React and encountered a new concept called a .d.ts file. I am wondering if there is an established best practice for writing this file or if it's more of a developer preference. Can someone verify if ...