【发布时间】:2020-11-07 05:44:20
【问题描述】:
当从 viewContainerRef 呈现数据时,如何将数据传递给子组件?在我的例子中,EventRegistrationComponent 有一个带有水龙头的导航栏,我用下面的渲染函数更改了 ng-template 组件。我可以切换到的每个组件都需要相同的事件信息
EventRegistrationComponent <ng-template appEventRegistration></ng-template>
@ViewChild(EventRegistrationDirective, { static: true }) registrationHost: EventRegistrationDirective;
otherFunction(){
this.render(EventRegistrationInfoComponent);
}
public render(component): void {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(component as any);
const viewContainerRef = this.registrationHost.viewContainerRef;
viewContainerRef.clear();
const comp = viewContainerRef.createComponent(componentFactory);
comp['event'] = this.event;
}
指令
@Directive({
selector: '[appEventRegistration]',
})
export class EventRegistrationDirective {
constructor(public viewContainerRef: ViewContainerRef) { }
}
子组件
export class EventRegistrationInfoComponent implements OnInit {
@Input() event: Event;
ngOnInit(): void {
console.log('Info Event');
console.log(this.event); //undefined
}
}
【问题讨论】:
标签: angular components