如何在容器中放置动态组件

编程入门 行业动态 更新时间:2024-10-27 08:24:25
本文介绍了如何在容器中放置动态组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建动态组件并将这些组件的视图插入到容器中.

I want to create dynamic components and insert views of these components to a container.

我认为可以通过 ViewContainerRef

但是我不知道,我们可以获取任何组件的ViewContainerRef吗?如果是,那么如何? 我是Angular的新手,如果有任何其他好的解决方案可以解决这种情况,请提出建议.

But I don't know, can we get ViewContainerRef of any component? if yes then how?. I am new to Angular, if there are any other good solutions available to handle this scenario, please suggest me.

已更新 我认为,我已经很接近解决方案了.下面是代码.

Updated I think, I am pretty much near to the solution. Below is the code.

appponent.ts

appponent.ts

import {Component} from '@angular/core'; import {ContainerComponet} from './containerponent' @Component({ selector: 'my-app', template: ` <container> </container> `, directives: [ContainerComponet] }) export class AppComponent { constructor() { } }

containerponent.ts

containerponent.ts

import {Component, ComponentResolver, ViewContainerRef} from '@angular/core' import {controlBoxComponent as controlBox} from './controlBoxponent'; @Component({ selector: 'container', template: 'container' }) export class ContainerComponet { constructor(viewContainer: ViewContainerRef, private _cr: ComponentResolver) { this._cr.resolveComponent(controlBox) .then(cmpFactory => { const ctxInjector = viewContainer.injector; return viewContainer.createComponent(cmpFactory, 0, ctxInjector); }) } }

controlBoxponent.ts

controlBoxponent.ts

import {Component} from '@angular/core' @Component({ selector: 'controlBox', template: 'controlBox' }) export class controlBoxComponent { constructor() { } }

输出

<my-app> <container>container</container><controlbox _ngcontent-lsn-3="">controlBox</controlbox> </my-app>

预期结果

<my-app> <container>container <controlbox _ngcontent-lsn-3="">controlBox</controlbox> </container> </my-app>

推荐答案

您可以通过或从当前组件视图中的元素获取当前组件的ViewContainerRef

You can get the ViewContainerRef of the current component by or from an element in the view of the current component

@Component({ selector: '...', directives: [OtherComponent, FooComponent], template: ` <other-component></other-component> <foo-component #foo></foo-component> <div #div></div>` }) export class SomeComponent { // `ViewContainerRef` from an element in the view @ViewChild(OtherComponent, {read: ViewContainerRef}) other; @ViewChild('foo', {read: ViewContainerRef}) foo; @ViewChild('div', {read: ViewContainerRef}) div; // `ViewContainerRef` from the component itself constructor(private viewContainerRef:ViewContainerRef, private componentFactoryResolver: ComponentFactoryResolver) {} let factory = thisponentFactoryResolver.resolveComponentFactory(ControlBox) thisponentRef = this.other.createComponent(factory); // thisponentRef = this.foo.createComponent(factory); // thisponentRef = this.div.createComponent(factory); // thisponentRef = this.viewContainerRef.createComponent(factory); }); }

另请参见 Angular 2动态标签用户单击选定的组件

更多推荐

如何在容器中放置动态组件

本文发布于:2023-10-08 09:59:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   组件   动态   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!