【问题标题】:How to Dynamically load component within a dynamically loaded list component如何在动态加载的列表组件中动态加载组件
【发布时间】:2018-04-07 15:55:35
【问题描述】:

我有一个组件,我使用下面的代码使用视图子和组件工厂动态加载了该组件

@Input() dynamicComponent: Observable<any>;
@ViewChild('expandablerow', { read: ViewContainerRef }) rowContainers;
    loadComponent( component) {
        if (component) {
          if (this.rowContainers > 0) { this.rowContainers.clear(); }
          const factory: ComponentFactory<any> = this.resolver.resolveComponentFactory(component);
          const inlineComponent = this.rowContainers.createComponent(factory);
        }
      }

但我在这里尝试实现的是替换以下列表之一 当用户单击黄色编辑按钮时带有详细信息组件。 我有一个功能可以根据单击的行获取数据,但我被困在那里

【问题讨论】:

  • 感谢我找到了解决方案

标签: angular angular5 dynamic-loading viewchild


【解决方案1】:

你真的需要动态加载吗?如果我是你,我会添加单个 &lt;app-details&gt;&lt;/app-details&gt; 组件并显示它而不是列表项。您可以使用 css order 属性将详细信息放在列表中的正确位置,并使用过滤管道将列表项本身从列表中排除。

或者,更简单的是,让列表项由两个组件组成:

<div *ngFor="let item of list">
    <div ngIf="item != clicked"></div>
    <div ngIf="item == clicked"></div>
</div>

这不应该影响性能,因为当 ngIf==true 组件实际上不存在时。

【讨论】:

  • 谢谢,但这里的用例要求我使用动态加载。我能够想出一个解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2019-10-19
  • 2013-06-22
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
相关资源
最近更新 更多