我需要将动态加载的内容添加到以下 Angular 中:
main.html
<div class="top">
<ng-template #main></ng-template>
</div>
main.ts
(主程序.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>"
希望能得到任何建议。
this.sanitizer.sanitize(SecurityContext.HTML, res)
来进行清理。 - Pipo<my-component></..>
或<div [myDirective]="something">
。 - Sh eldeeb