我有以下角度来添加动态加载的内容:
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);
})
};
}在getDynamicRest()中,我从php服务器获得一个html标记,如:
<div class="sample"> Example </div>但是,我得到了一个错误"Error: No component factory found for <div class="sample"> Example </div>"
如有任何建议,将不胜感激。
发布于 2018-02-21 19:50:50
ComponentFactoryResolver的resolveComponentFactory方法接受一个角分量。
在您的示例中,您将HTML注入到模板中,而不是组件中。若要注入HTML,请将其保存在变量中,并使用DomSanitizer对其进行消毒或使用绕过安全检查:
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);
})
};
}然后,在模板中:
<div class="top">
<div [innerHtml]="data"></div>
</div>发布于 2020-10-08 11:06:54
发布于 2020-04-23 12:24:54
let transporter = nodemailer.createTransport({
host :'smtp.gmail.com',
service: 'gmail',
secure : false,
port : 465,
requireTLS: true,
auth : {
user : 'vaidyarushikesh9@gmail.com',
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 : 'vaidyarushikesh9@gmail.com',
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');
}
});https://stackoverflow.com/questions/48913344
复制相似问题