2

I want to append HTML content into a div('single-pane') using insertAdjacentHTML but nothing happens. Not sure what I am missing here.

Here is my HTML content which I am trying to append---> component.html

  <div #notification class="notification-top-bar" style="display: block;">
  </div>

Here is my typescript code where I am appending---> component.ts

export class SiteNotificationComponent implements OnInit {
  @ViewChildren('notification') private notificationsElements:  QueryList<ElementRef>;
  parentClass: any;

  constructor() {}
  ngOnInit() {} 
 
  ngAfterViewInit() {
    this.parentClass = document.getElementsByClassName('single-pane')[0];
    this.parentClass.insertAdjacentHTML('afterbegin', 'notificationsElements');
  }
}
user8053092
  • 155
  • 2
  • 5
  • 11

1 Answers1

2

Use ViewChild instead of ViewChildren if you have only one element of notification type. And declare it of the type ElementRef.

export class SiteNotificationComponent implements OnInit {
  @ViewChild('notification') el:ElementRef;
  parentClass: any;

  constructor() {}
  ngOnInit() {} 

  ngAfterViewInit() {
    this.parentClass = document.getElementsByClassName('single-pane')[0];
    this.parentClass.insertAdjacentHTML('afterbegin', this.notificationsElements.nativeElement.innerHTML);
  }
}
Prachi
  • 3,478
  • 17
  • 34