For example i have something like this:
FancyComponentWantsToShowAnOverlay.ts
...
ClickToDoSomething(): void {
// doing something
}
MoveOrCloneToBody(): void {
// do the magic
}
...
FancyComponentWantsToShowAnOverlay.html ...
* displaying fancy stuff *
<ng-template #MoveOrCloneMeToBodyPlease>
* seriously beautiful stuff will be displayed here *
</ng-template>
* displaying more fancy stuff *
<a (click)="MoveOrCloneToBody()">do the magic</a>
MoveOrCloneMeToBodyPlease should be appended to body (removing also would be nice later).
Thanks!
Dynamically Add/Remove Html Elements
*ngFor
You can create a component of the 'something beautiful' and use the ngFor directive to loop over an array which you can add or remove elements to/from. All the eventlisteners would be in the components that are added.
Template:
Script:
Here is an example in a Stackblitz
Renderer2
You can also use renderer2 to manipulate the DOM, where you can bind to the element you want to append children to using
@ViewChild
annotation. You can add listeners to elements using renderer2, so this would also fit your purpose.Template:
Script:
Here is another example in a Stackblitz.
InnerHTML
You can also bind to the innerHtml from any html element. But, this would not contain any eventlisteners so wouldn't solve your problem.
Template:
Script:
And yet another example in a Stackblitz.