[英]How to dynamically add component BEFORE another list element in Angular 2+
Let's say I have a list of items, LI style, like a contact list or to-do things like this: 假设我有一个LI风格的项目列表,例如联系人列表或要做的事情,例如:
<li data-letter="J">James Taylor</li>
<li data-letter="J">John Connor</li>
<li data-letter="K">Kyle Connor</li>
<li data-letter="P">Peter Parker</li>
And I need to dynamically add a letter separator between some of those elements, where the result should be something like this: 而且我需要在其中一些元素之间动态添加字母分隔符,其结果应该是这样的:
<separator-component>- J -</separator-component>
<li data-letter="J">James Taylor</li>
<li data-letter="J">John Connor</li>
<separator-component>- K </separator-component>
<li data-letter="K">Kyle Connor</li>
<separator-component>- P -</separator-component>
<li data-letter="P">Peter Parker</li>
Using ViewChildren
I have managed to get the list of QueryList<ViewContainerRef>
for those LI items, and could add the separator components to them, but only after the item I want to insert BEFORE . 通过使用
ViewChildren
我设法获取了这些LI项目的QueryList<ViewContainerRef>
列表,并且可以向其中添加分隔符组件,但是仅在要插入BEFORE的项目之后 。
This is my component template: 这是我的组件模板:
<li #listItems *ngFor="let element of elements" [attr.data-letter]="element.letter">{{ element.name }}</li>
And this is the code (the relevant part of it): 这是代码(代码的相关部分):
@ViewChildren('listItems', {read: ViewContainerRef}) listItems: QueryList<ViewContainerRef>;
addItem(index: number) {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(SeparatorComponent);
const componentRef = this.listItems.toArray()[index].createComponent(componentFactory, 0);
componentRef.changeDetectorRef.detectChanges();
this.letterRefs.push(componentRef);
}
And this is the result I'm getting so far... 这就是我到目前为止的结果...
<li data-letter="J">James Taylor</li>
<separator-component>- J -</separator-component>
<li data-letter="J">John Connor</li>
<li data-letter="K">Kyle Connor</li>
<separator-component>- K </separator-component>
<li data-letter="P">Peter Parker</li>
<separator-component>- P -</separator-component>
Any suggestions? 有什么建议么?
This seems to work: 这似乎可行:
<ng-container *ngFor="let element of elements" #listItems>
<li [attr.data-letter]="element.letter">{{ element.name }}</li>
</ng-container>
Edit : After closer inspection this works because ng-container
becomes a comment in the DOM before the li
: 编辑 :经过仔细检查,这是有效的,因为
ng-container
在li
之前成为DOM中的注释:
<!---->
<li></li>
And createComponent
always renders the new component after (not inside) the ViewContainerRef
, which is the comment in this case. 并且
createComponent
始终在ViewContainerRef
之后 (而不是内部)呈现新组件,在这种情况下为注释。
So it's kind of like doing the following: 因此有点像执行以下操作:
<ng-container *ngFor="let element of elements">
<ng-container #listItems></ng-container>
<li [attr.data-letter]="element.letter">{{ element.name }}</li>
</ng-container>
Also, the 0
in this line can be removed because we don't care about an index anymore: 另外,可以删除此行中的
0
,因为我们不再关心索引了:
this.listItems.toArray()[index].createComponent(componentFactory, 0);
I actually hope someone has a better answer to this question because it feels like a hack! 实际上,我希望有人对这个问题有更好的答案,因为这感觉像是骇客!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.