Angular2子组件
我要创建以下组件:Angular2子组件,angular,typescript,Angular,Typescript,我要创建以下组件: <my-component [attr1]="attr1" [attr2]="attr2"> </my-component> 这很好,但是我想在这个组件中呈现任何类型的模板,所以我需要如下内容: <my-component [attr1]="attr1" [attr2]="attr2"> <my-component-template> // Here comes the template for
<my-component [attr1]="attr1" [attr2]="attr2">
</my-component>
这很好,但是我想在这个组件中呈现任何类型的模板,所以我需要如下内容:
<my-component [attr1]="attr1" [attr2]="attr2">
<my-component-template>
// Here comes the template for my-component component
</my-component-template>
</my-component>
//下面是我的组件的模板
如何做到这一点的任何样本?我找不到任何参考。我想您正在寻找
或使用ViewContainerRef
和ComponentFactoryResolver
添加组件。我在官方文件中找不到任何有用的例子
请参见使用的
:
my cmp
和my cmp模板
它们都是不同的组件吗??“任何东西”都有很大的不同。如果它包含角度信息,则需要在运行时对其进行编译,如果它不是用代码编译的。关于这一点,有问题也有答案。如果它不包含有角度的内容,您可以像[prop]=“someHtml”
一样传递HTML,然后在组件内部使用您想要向DOM添加HTML的任何内容(创建一个DocumentFragment`并添加它。好吧,那么你的意思是不可能创建一个组件嵌套?我可以按照你的建议做,但这不是我的问题。如果我按照建议使用属性绑定(我已经有了这种方法),这可能看起来很可怕,这取决于内容的HTML有多大,