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');
  }
}

1 Answer 1

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);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried what you have suggested, it appends [object HTMLDivElement]
Try this.notificationsElements.nativeElement.innerHTML. I have updated the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.