I got a page where I try to insert HTML with scripts.
import {
Component,
AfterViewInit,
ViewChild,
ElementRef
} from '@angular/core';
@Component({
selector: 'menu-blocks-menupage',
template: `<div class="menuArticle" #dataContainer (click)="logit()" padding></div>`
})
export class MenuBlocksMenuPage implements AfterViewInit {
menu: MenuItem;
//Контейнер для вставки
@ViewChild('dataContainer') dataContainer: ElementRef;
ngAfterViewInit() {
this.dataContainer.nativeElement.innerHTML = "<script type='text/javascript'>" + "alert('123123213');" + "</script>";
}
}
HTML with script tags is inserted after render, but nothing happens. What is wrong?
ANSWER IS:
ngAfterViewInit()
{
const fragment = document.createRange().createContextualFragment("<script type='text/javascript'>" +
"alert('123123213');" +
"</script>");
this.dataContainer.nativeElement.appendChild(fragment);
}