Angular 4 - 如何显示继承类组件的模板

2024-01-03

我正在尝试根据项目(组件)的类型显示项目列表:

我有一系列组件。 全部继承自基类。 数组类型被定义为基类的类型。 我想显示数组(比如说作为项目列表),每个数组都有自己的模板,而不是基本模板。

我已经尝试过:

在app.component.html中:

<app-shape *ngFor="let shape of shapes"></app-shape>

在 app.component.ts 中:

shapes: ShapeComponent[] = [new CircleComponent(), new SquareComponent()];

我定义了 3 个组件:

export class ShapeComponent {
}

export class CircleComponent extends ShapeComponent{
}

export class SquareComponent extends ShapeComponent{
}

结果是我得到了一个形状列表。

Angular 支持这样的事情吗?

Thanks!


声明式方法

您可以使用ngComponentOutlet https://angular.io/api/common/NgComponentOutlet.

Code:

shapes: ShapeComponent[] = [CircleComponent, SquareComponent];

模板:

<ng-container *ngFor="let shape of shapes">
    <ng-container *ngComponentOutlet="shape">
    </ng-container>
</ng-container>

ngComponentOutlet- 实例化单个组件类型并插入 将其宿主视图转换为当前视图。NgComponentOutlet提供了一个 动态组件创建的声明性方法。

NgComponentOutlet如果设置了错误值,则需要组件类型 视图将清除,任何现有组件将被销毁。

因此,模板中不需要硬代码。*ngFor将迭代代码中的组件类型数组

Update

不记得添加动态渲染组件entryComponents of 应用程序模块:

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, HelloComponent, AComponent, BComponent ],
  entryComponents: [
    AComponent, BComponent 
  ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

StackBlitz 演示 https://stackblitz.com/edit/angular-compoent-outlet

设置数据的命令式方法

应用程序组件.模板:

<ng-container #comps>

</ng-container>

访问#comps(ng-container) 查看者ViewChild装饰器并创建组件。 所以你不能像这样初始化组件b = new BComponent().

  1. 首先需要解决组件工厂。
  2. 通过初始化组件视图容器引用 https://angular.io/api/core/ViewContainerRef#description 创建组件 https://angular.io/api/core/ViewContainerRef#createComponent方法。它返回参考 https://angular.io/api/core/ComponentRef到实例化组件
  3. 通过引用,访问实例属性并根据需要更新任何数据

应用程序组件.ts:

 @ViewChild('comps', { read: ViewContainerRef }) comps: ViewContainerRef;
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {

  }

ngOnInit() {
    this.comps.clear();
    let aComponentFactory = this.componentFactoryResolver.resolveComponentFactory(this.compArr[0]);
    let aComponentRef = this.comps.createComponent(aComponentFactory);
    (<AComponent>aComponentRef.instance).name = 'A name';

    let bComponentFactory = this.componentFactoryResolver.resolveComponentFactory(this.compArr[1]);

    let bComponentRef = this.comps.createComponent(bComponentFactory);
    (<BComponent>bComponentRef.instance).name = 'B name';
  }

StackBlitzh 演示 https://stackblitz.com/edit/angular-dynamic-components-load

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 4 - 如何显示继承类组件的模板 的相关文章

随机推荐

  • 增强同步

    我有 NUM THREADS 个线程 线程中包含以下代码 Calculate some value Critical section to accummulate all thresholds boost mutex scoped lock
  • 如何创建自定义 javadoc 标签?

    如何创建自定义 javadoc 标签 例如 pre post 我找到了一些解释它的链接 但我没有运气 以下是一些链接 http www developer com java other article php 3085991 Javadoc
  • 尝试通过 iPhone 模拟器播放声音

    我正在尝试从 iPhone 程序播放声音文件 这是代码 NSString path NSBundle mainBundle pathForResource play ofType caf NSFileHandle bodyf NSFileH
  • Bigquery python SchemaField() 与结构数组

    我正在尝试通过 python 客户端在 Bigquery 创建一个表 文档使用bigquery SchemaField name TYPE 定义一个字段 虽然它似乎不起作用ARRAYS or STRUCTS 这是我试图创建的 STRUCTS
  • data.table 根据组的滞后值删除行

    我有一个data table形式如下 DT lt data table tag rep c A B each 10 value c 0 3 3 3 0 1 1 1 3 0 0 1 3 1 0 3 0 1 1 0 gt DT tag valu
  • 连接 Apache Spark DataFrame 中的列

    我们如何连接 Apache Spark DataFrame 中的两列 Spark SQL中有没有我们可以使用的函数 通过原始 SQL 您可以使用CONCAT 在Python中 df sqlContext createDataFrame fo
  • 使用其他 gradle 文件中的 Gradle 函数

    我想将 300 行 build gradle 逻辑地拆分为多个构建文件 以使其更易于维护和扩展 正如我所注意到的 可以将 gradle 任务拆分为多个文件并将它们用于 apply from myGradleFile 遗憾的是 通过这种方法
  • 等待函数中的 Ajax 调用结束,然后将对象返回给外部变量

    我想按照预期使用 JavaScript 异步 我想将收到的数据 对象分配给我需要的尽可能多的变量 DataModel01 DataModel02 DataModel03 等 我的想法是 我对 API 数据的需求一直在变化 我只想定义一次从哪
  • 无需 IP 即可访问 R Shiny 应用程序

    我开发了一个 R Shiny 应用程序并托管在本地 Intranet 服务器上 我的同事可以使用 IP Port 来访问它 不像http 192 168 135 146 5050 http 192 168 135 146 5050 但是 如
  • django local_settings导入错误

    我想导入 local settings py 文件 不在我的 VCS 系统中 以覆盖 settings py 中的数据库设置 为此 我在 settings py 文件的最后添加了这些行 try from local settings imp
  • 就地映射 NumPy 数组

    是否可以将 NumPy 数组映射到位 如果是 怎么办 Given a values 2D 数组 这是目前对我有用的代码 for row in range len a values for col in range len a values
  • 旧浏览器是否支持 HTML 5 数据属性?

    我将一些自定义数据存储在 HTML5 数据属性中以进行 Jquery 处理 自定义数据属性在旧版浏览器中可用吗 HTML5datalist属性在较旧的浏览器中不可用 尽管它可以很容易地填充 您始终可以使用标准getAttribute方法而不
  • 增加 Xcode“最近项目”列表的长度?

    我经常在 Xcode 中切换处理许多不同的项目 有些是我正在积极处理的项目 有些是我正在查找代码的旧项目 我想在 SO 答案中重用或引用 这样我的 工作集 项目的一部分最终总是从最近的项目列表中消失 我确实使用查找器选项卡来查看当前项目文件
  • Asyncjs:绕过瀑布链中的函数

    我想从瀑布函数链中跳转一个函数asyncjs in nodejs 我的代码如下所示 async waterfall function next if myBool next null else Bypass the 2nd function
  • postgresql 中多列上的多个索引与单个索引

    阅读有关该主题的一些现有帖子 我无法得出任何结论性的答案 我有过去 10 年 100 个地点的某些数据 该表约有 8 亿行 我需要主要生成每个位置的年度统计数据 有时我还需要生成每月变化统计数据和每小时变化统计数据 我想知道是否应该生成两个
  • Angular2 应用程序:当 Google 不加载页面内容时进行获取

    我正在开发基于 Angular2 的网络应用程序 我使用 Angular CLI 生成应用程序 然后为产品构建它 我在 AWS S3 和 Cloudfront 上托管了网站 当我使用网站管理员提供的 Fetch as Google 工具时
  • 测试 OSGi 包的快速简便方法

    目前 我正在开发一个新的版本控制系统 作为大学最后一年项目的一部分 我们的想法是使其具有高度适应性和可插拔性 我们使用 OSGi 框架 Equinox 实现 来管理我们的插件 我的问题是我找不到简单易用的方法来测试 OSGi 包 目前 我必
  • ILASM 未设置文件版本

    我有一个 il 文件 可以毫无问题地编译它 我可以很清楚地命名它 所以没有任何问题 但我无法按照我的预期通过属性设置文件版本 使用 ilasm 时如何设置程序集的文件版本 如果我进行往返 我总是会得到一个 res 文件 该文件仅包含不可读的
  • 在 Celery 链中使用分组结果

    我陷入了相对复杂的芹菜链配置 试图实现以下目标 假设有如下一系列任务 chain1 chain DownloadFile s http someserver file gz downloads file returns temp file
  • Angular 4 - 如何显示继承类组件的模板

    我正在尝试根据项目 组件 的类型显示项目列表 我有一系列组件 全部继承自基类 数组类型被定义为基类的类型 我想显示数组 比如说作为项目列表 每个数组都有自己的模板 而不是基本模板 我已经尝试过 在app component html中