I have following angular to add dynamically loaded content:
main.html
<div class="top">
<ng-template #main></ng-template>
</div>
main.ts
import { Component, ViewChild, ComponentFactoryResolver, ViewContainerRef } from '@angular/core';
@Component({
selector: 'page-main_page',
templateUrl: 'main_page.html'
})
export class main_page {
@ViewChild('main', { read: ViewContainerRef }) entry: ViewContainerRef;
data: any;
constructor(public resolver: ComponentFactoryResolver){
};
ngOnInit(){
this.getDynamicREST().then((res)=>{
this.data = res; //Where it is a html markup from php server: <div class="sample"> Example </div>
const factory = this.resolver.resolveComponentFactory(this.data);
this.entry.createComponent(factory);
})
};
}
In the getDynamicRest(), I am getting a html markup from php server such as :
<div class="sample"> Example </div>
However I am getting an error "Error: No component factory found for <div class="sample"> Example </div>"
Any suggestions would be much appreciated.
3 Answers 3
The ComponentFactoryResolver's resolveComponentFactory method accepts an Angular Component.
In your case, you are injecting HTML into your template, not a component. To inject HTML, save it in a variable and use the DomSanitizer to either sanitize it or bypass the security check:
export class main_page {
data: SafeHtml;
constructor(private sanitizer: DomSanitizer) {}
ngOnInit(){
this.getDynamicREST().then((res)=> {
this.data = this.sanitizer.sanitize(res);
/* OR */
this.data = this.sanitizer.bypassSecurityTrustHtml(res);
})
};
}
Then, in your template:
<div class="top">
<div [innerHtml]="data"></div>
</div>
11 Comments
this.sanitizer.sanitize(SecurityContext.HTML, res)<my-component></..> or <div [myDirective]="something"> let transporter = nodemailer.createTransport({
host :'smtp.gmail.com',
service: 'gmail',
secure : false,
port : 465,
requireTLS: true,
auth : {
user : '[email protected]',
pass : 'PAssword',
}
});
var email = req.body.email`enter code here`;
let htmlContent = `<h1><strong>Foreget Password Link Form</strong></h1>
<p>Hi,</p>
<p>http://localhost:4200/forget/${email} contacted with the following Details</p>`;
let mailoptions = {
from : '[email protected]',
to : req.body.email,
subject : 'tesst',
text: '',
html: htmlContent
};
transporter.sendMail(mailoptions,function(err,data){
if(err){
console.log('errr',err)
}else{
console.log('email send');
}
});