我尝试过很多像stackoverflow的选项 动态加载现有组件Angular 2 Final Release。
我想要做的是获取一个带有ajax请求的html页面,并在我的自定义组件中渲染/编译此模板。
我已经发现angular2有两个已弃用的组件,我必须使用它 ComponentFactoryResolver。
在我的旧解决方案中,我可以设置'[innerHtml]'来呈现HTML。 现在我需要一个新的解决方案。
谁能帮助我?
page.component.ts
import { Component, ViewChild, ViewContainerRef, ComponentFactory, OnInit, ComponentFactoryResolver } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
@Component({
selector: "wd-page",
templateUrl: "/app/page/page.component.html",
providers: []
})
export class PageComponent implements OnInit {
// we need the viewcontainer ref, so explicitly define that, or we'll get back
// an element ref.
@ViewChild('dynamicChild', { read: ViewContainerRef })
private target: ViewContainerRef;
private page = {
Source: "<div><h2>Hello world</h2><one-of-my-components></one-of-my-components></div>"
}
constructor(
private vcRef: ViewContainerRef,
private resolver: ComponentFactoryResolver) { }
ngOnInit() {
//What code do i need here?
}
}
<div #dynamicChild></div>
<!-- Old implementation!
<div *ngIf="!showSource" [innerHTML]="page">
</div>
-->