繁体   English   中英

在 Angular TS 文件中返回组件选择器(Angular 2+)

[英]Return component selector inside Angular TS file (Angular 2+)

假设我的 TS 文件中有一个方法可以返回一些被注入到模板中的 html。 我想在退货中包含另一个组件,但我无法让它工作。

例如说我的 TS 文件中有以下方法:

returnComponent() {
   return `<my-component></my-component>`
}

每次我运行这个方法时,它似乎只返回 html 标签,而不是实际的组件。 所以在运行该方法后我得到

<div>
   <my-component>
      <!-- nothing here -->
   </my-component>
</div>

我想得到

<div>
   <my-component>
      <!-- the contents of 'my-component' -->
   </my-component>
</div>

我错过了什么? 或者这甚至不可能?

您实际上正在寻找的是动态模板。 您不能将动态模板作为字符串值。 模板必须位于任何组件的 html 内,您可以引用该模板ng-template

所以你需要改变你的实现。 它的过程并不像你想象的那么简单。

按照这篇文章https://medium.com/@DenysVuika/dynamic-content-in-angular-2-3c85023d9c36

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM