Angular,从动态创建的Component获取ViewChild / ViewContainerRe
发布时间:2020-12-17 18:06:13 所属栏目:安全 来源:网络整理
导读:有没有办法从动态创建的组件中获取ViewContainerRef? 我的动态创建组件里面有一个ngContent元素,我想在动态创建后填充它. export class Example { @ViewChild('content',{ read: ViewContainerRef }) //normal way content: ViewContainerRef;... ngAfterVi
有没有办法从动态创建的组件中获取ViewContainerRef?
我的动态创建组件里面有一个ngContent元素,我想在动态创建后填充它. export class Example { @ViewChild('content',{ read: ViewContainerRef }) //normal way content: ViewContainerRef; ... ngAfterViewInit(){ let box1=this.content.createComponent(this.boxFactory); box1.instance.title="Dynamic (1)"; // HERE: the box1 has a ng-content area which i want to fill. } } 我想一些解决ViewChild而不是使用装饰器的手动方法.一些想法? 非常感谢. 解决方法
我不确定如何动态添加viewchild.但是,如果动态ViewContainerRef是您想要的,您可以尝试使用此解决方案:
在html文件中创建: <div id="test"></div> 在您的组件中创建: let nodeElement = document.getElementById("test"); let compFactory = this.componentFactoryResolver.resolveComponentFactory(ChildComponent); let component = compFactory.create(this.viewContainerRef.injector,null,nodeElement); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- AIX上报:“Shell will time out in 60 seconds” 解决办法
- 在shell脚本中检测python版本
- AT91SAM9260EK的BootStrap移植
- docker – 如何在本地机器和minikube之间传输文件?
- webservice脚本开发实战
- bash – printf中的填充字符
- 【WebService】第二章、XML文件命名空间以及约束
- angularjs – 如何传递url参数(查询字符串)到Angular 2上的
- 【错误处理】Angular报错:ERROR in MessageDetailComponen
- angularjs – 在Angular JS中使用$timeout而不是window.set