Dynamically append component to div in Angular 5
Here's the way I got it working
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>
It's pretty simple. I just made an example to you.
Please, read the comments inside loader directive.
https://github.com/garapa/studying/tree/master/loader
EDIT:
You 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();
}
}
}
You module
@NgModule({
...
entryComponents: [
LoaderComponent
]
})