Angular 2 - 让子组件加载到父组件中(动态创建)

标签 angular

我正在尝试动态创建 header ,构造 header 模板并将其作为“字符串”传递给我的动态组件。我的要求是在 header 组件模板字符串中调用另一个组件并加载它。

已创建一个工作 Plunker这里是场景。

我在浏览器控制台中收到错误:

Unhandled Promise rejection: Template parse errors:
'alert' is not a known element:
1. If 'alert' is an Angular component, then verify that it is part of this module.
2. If 'alert' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("     <span class="title" style="float: right; margin-right: 10px;">
                                [ERROR ->]<alert></alert>   
                                <img class="notify-circle" src="https://cdn0.iconf"): DynamicComponent@2:32 ; Zone: <root> ; Task: Promise.then ; Value: Object { _nativeError: Error, stack: "" } BaseError@https://unpkg.com/@angular/compiler/bundles/compiler.umd.js:1595:29 [angular]

问题:

如何使用选择器 '<alert></alert>' 在构造为字符串的 header 模板中识别“警报”组件.

这个概念可能看起来很奇怪,但要求就是这样。

最佳答案

您可以创建特殊的共享模块,其中始终包含您需要的内容

@NgModule({
  declarations: [AlertComponent],
  exports: [AlertComponent]
})
export class SharedDynamicModule {}

然后将其导入到您的DynamicModule

dynamic.ts

@NgModule({
  imports: [ CommonModule, SharedDynamicModule ],
  declarations: [ DynamicComponent ]
})
class DynamicHtmlModule { }

<强> Modified Plunker

关于Angular 2 - 让子组件加载到父组件中(动态创建),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41565275/

相关文章:

node.js - Angular 4 生产部署工作流程

javascript - 将对象组合成新对象作为 json 用于 web api 请求的发布

forms - 子组件的 Angular Form 验证

android - 如何在 WhatsApp Business 应用程序中打开 wa.me 链接,而不是在同一部手机上打开个人 WhatsApp?

angular - ng build --prod --base-href

angular - 如何在 Angular 后调用中正确发送 application/x-www-form-urlencoded

angular - 使用 rxjs 将数据添加到 http 响应

angular - native ionic 网络状态不适用于我的代码

angular - 如何获取查询参数? Angular 2

angular - import * as angular from 'angular' inside app.module.ts 文件