[英]Angular: Custom Component in Custom Component
After lots of searching: Is it possible to nest custom components in custom components?经过大量搜索:是否可以在自定义组件中嵌套自定义组件?
I've created an empty angular cli - project and created ComponentAComponent (selector: 'componentA')
and ComponentBComponent (selector: 'componentB')
.我创建了一个空的 angular cli - 项目并创建了
ComponentAComponent (selector: 'componentA')
和ComponentBComponent (selector: 'componentB')
。
My app.component.html looks like:我的 app.component.html 看起来像:
<componentA>
<componentB></componentB>
<componentB></componentB>
</componentA>
<componentB></componentB>
What I would expect:我期望的是:
Component A works!
组件 A 有效!
Component B works!组件 B 有效!
Component B works!组件 B 有效!
Component B works!组件 B 有效!
But only但只有
Component A works!
组件 A 有效!
Component B works!组件 B 有效!
gets rendered.被渲染。 As I want to implement a custom list with custom list-items: What do I have to do to render custom components nested in custom components?
由于我想使用自定义列表项实现自定义列表:我必须做什么才能呈现嵌套在自定义组件中的自定义组件?
After a lot of google/ stackoverflow/ trial and error - grateful for any advice or link you can give me.经过大量的 google/stackoverflow/ 反复试验 - 感谢您给我的任何建议或链接。 Tried so much stuff with imports and declarations but nothing worked.
用导入和声明尝试了很多东西,但没有任何效果。
In case it helps I can provide a github repository with code.如果有帮助,我可以提供一个带有代码的 github 存储库。
Kind regards亲切的问候
EDIT: ABOS provided the correct answer.编辑: ABOS 提供了正确答案。 I added
我加了
<ng-content></ng-content>
to componentA.html and now all nested elements get rendered.到 componentA.html,现在所有嵌套元素都被渲染。 So simple, but I did not find it.
这么简单,但我没有找到。 Thank you!
谢谢!
If you want to add html/components dynamically, you can use <ng-content>
.如果要动态添加 html/components,可以使用
<ng-content>
。 There are tons of tutorials on it, eg https://blog.angular-university.io/angular-ng-content/有很多关于它的教程,例如https://blog.angular-university.io/angular-ng-content/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.