代码之家  ›  专栏  ›  技术社区  ›  cup_of

返回角度ts文件内的组件选择器(角度2+)

  •  0
  • cup_of  · 技术社区  · 6 年前

    假设我在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>
    

    我错过了什么?或者这根本不可能?

    1 回复  |  直到 3 年前
        1
  •  1
  •   Sunil Singh    6 年前

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

    所以你需要改变你的实现。这并不像你想的那么简单。

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