我正在尝试用ElementRef将多个子组件注入父组件,但是当我试图获得子组件html时出了问题。
我的孩子部分
html: string = '';
constructor(private elementRef: ElementRef) { }
ngAfterContentInit() {
this.html = this.elementRef.nativeElement;
}我想在我的父组件中得到那个子HTML
var textComponent = new TextStepComponent();
console.log(textComponent.html); << child component我该怎么做?或者另一种方式?
我想要的所有东西都是在另一个组件中获取一个组件的HTML
救命啊!!
发布于 2017-07-11 05:02:22
我用这种方式解决了我的问题:
我的父组件:
import { TextStepComponent } from './components/text/text.component';
constructor(
private viewContainerRef: ViewContainerRef,
private componentFactoryResolver: ComponentFactoryResolver,
) { }
ngAfterViewInit() {
const componentFactory =
this.componentFactoryResolver.resolveComponentFactory(TextStepComponent);
const containerRef =
this.viewContainerRef.createEmbeddedView(this.dataContainer);
console.log(containerRef);
const dynamicComponent =
containerRef.createComponent(componentFactory).instance;
dynamicComponent.data = { content: 'xxx' };
}https://stackoverflow.com/questions/45024925
复制相似问题