Angular1 provides angular.element by to create JavaScript element. In Angular2, It is much easy, you can define element by using #<element name> as attribute

Markup to define element

<div #myElement>
  sample text in div
</div>

Emptying Element

You can empty element by using making whole inner HTML as null. Find below markup
<button (click)="myElement.innerHTML = ''">Empty div</button>

Remove Element

You can remove element by using .remove()
<button (click)="myElement.remove()">Empty div</button>
View DEMO

3 comments:

  1. Simple but perfect for what I needed. Thanks!

    ReplyDelete
  2. Great for the template way, but how can i do that in the component ts?.

    Thanks

    ReplyDelete
  3. @elpana :

    @ViewChild('myElement') private myElementContainer:ElementRef;

    this.myElementContainer.nativeElement.innerHTML = '';

    add this to your code.

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts