Angular 访问动态创建的输入
我有以下代码Angular 访问动态创建的输入,angular,angular2-forms,Angular,Angular2 Forms,我有以下代码 createPhone(): void { const widgetFactory = this._resolver.resolveComponentFactory( PhoneComponent ); this.componentRef = this.container.createComponent( widgetFactory ); this.componentRef.instance.containerRef = this.conta
createPhone(): void {
const widgetFactory = this._resolver.resolveComponentFactory( PhoneComponent );
this.componentRef =
this.container.createComponent( widgetFactory );
this.componentRef.instance.containerRef = this.container;
}
在创建/附加动态输入的PoneComponent中,我可以在PhoneComponent的哪个生命周期方法中访问this.componentRef.instance.containerRef
我得到了一个未定义的每个生命周期使用的方法
谢谢您可以在任何生命周期方法中使用它,除了
ngOnChanges
,因为在调用任何生命周期方法之前,您的上述代码都会赋值。
未调用ngOnChanges
,因为动态添加的组件不能有任何绑定
此外,构造函数也不能使用,因为赋值是在构造组件之后完成的
确保定义了此.container
您可以在任何生命周期方法中使用它,除非
ngOnChanges
,因为在调用任何生命周期方法之前,您的上述代码都会赋值。
未调用ngOnChanges
,因为动态添加的组件不能有任何绑定
此外,构造函数也不能使用,因为赋值是在构造组件之后完成的
确保定义了此.container