[英]Angular 4 using ngComponentOutlet to display dynamically changing ContentChildren
My ultimate goal is to just have a custom container component with a dynamic list of custom card components that are individually accessible (not as a single entity using ng-content). 我的最终目标是拥有一个自定义容器组件,其中包含可单独访问的自定义卡组件的动态列表(而不是使用ng-content的单个实体)。
<custom-card-holder> <custom-card></custom-card> <custom-card></custom-card> ... <custom-card></custom-card> </custom-card-holder>
Ideally I just grab the child components with ContentChildren query and put each one into a styled div inside the custom card holder. 理想情况下,我只是使用ContentChildren查询来获取子组件,然后将每个组件放入自定义持卡人内部的样式div中。
The displaying each content child individually in the styled div to get it to fit nicely in the holder. 在样式div中单独显示每个内容子级,以使其很好地适合持有人。
I have tried using TemplateOutlet and a few other odds and ends but the ComponentOutlet seems the most promising. 我试过使用TemplateOutlet和其他一些零碎的东西,但是ComponentOutlet似乎是最有前途的。
I would like to avoid any extra clutter on the code shown above. 我想避免上面显示的代码出现任何额外的混乱情况。 I understand the data can just be input and the component can be used inside without any transclusion but that just means a more complex interface for others to figure out.
我知道可以直接输入数据,并且可以在内部使用该组件而无需任何包含,但这仅意味着其他人可以找到更复杂的接口。
@Arivinds comment here led to fixed a syntax problem and also a semi-workaround for the original issue. @Arivinds的评论在此导致修复了语法问题,并且还解决了原始问题的半解决方法。
Nesting the Class in a list with the instance: 将Class与实例嵌套在列表中:
cardList = [{ component:CardClass,context:instance}]
In the end I added a CardHolder
component which template is just a <ng-content></ng-content>
and instead of Card
class I had CardHolder
in the cardList
. 最后,我添加了
CardHolder
组件,该模板只是一个<ng-content></ng-content>
,而不是Card
类,我在CardHolder
中添加了cardList
。
Then I used the content property of ngComponentOutlet
to project my actual Card
into the CardHolder
. 然后,我使用
ngComponentOutlet
的content属性将实际的Card
CardHolder
到CardHolder
。
cardList:any[] = [
{
type:CardHolder,
context:{}
}
];
`<div #scrollItem class="item" *ngFor="let card of cardList">
<ng-container *ngComponentOutlet="card.type;content: card.context;"></ng-
container>
</div>`
The catch is that content(ngComponentOutlet) takes a Node so When I query for ContentChildren
I purposefully grabbed the ElementRef
and used nativeElement
. 问题是content(ngComponentOutlet)带有一个Node,所以当我查询
ContentChildren
我有目的地抓住ElementRef
并使用nativeElement
。 Note the double square brackets. 请注意双方括号。
`ngAfterContentInit(){
this.cardList = this.cards.toArray().map((card)=>{
return {type:CardHolder,context:[[card.nativeElement]]};
})
}`
Hope this helps anyone in the same boat. 希望这可以帮助同一条船上的任何人。
Note : The ability to add inputs to dynamic components is supposedly in the works/backlogged but currently not implemented 注意 :可以在动态/待完成的工作中添加输入到动态组件的功能,但目前尚未实现
Note : Also I believe any dynamic components must be in the entryComponents array of the NgModule 注意 :我也相信任何动态组件都必须在NgModule的entryComponents数组中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.