I have a CMS that stores some HTML contents. In my Angular web app I am getting these contents and injecting them into my templates.
Let's say "myContent" content get from the model is:
<div>Test</div>
<script type="text/javascript">alert("test")</script>
In the component that use this content I have done:
export class PlaceholderComponent implements OnInit {
@Input() placeholderKey : string;
private placeHolderContent : SafeHtml;
constructor(private modelProvider: ModelProviderService, private sanitizer: DomSanitizer){}
ngOnInit() {
if(this.model.placeholders) {
this.placeHolderContent = this.sanitizer.bypassSecurityTrustHtml(this.model.placeholders[this.placeholderKey]);
}
}
}
And in the template of this component
<div [innerHtml]="placeHolderContent"></div>
As a result, the html is correctly injected in the template, including the script tag, but the alert is never executed (note that console.log("test") is not working either).
Do you have a solution to inject HTML with JavaScript that would be run when the component finish loading?
(Note: if I use jquery inside my Angular component to insert HTML in the template it works!)