简体   繁体   English

如何在Angular 2+中的另一个列表元素之前动态添加组件

[英]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-containerli之前成为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.

相关问题 Angular 2+添加或删除属于另一个组件的元素类 - Angular 2+ Add or Remove a Class of an element that belongs to another component 从Angular 2+中的同级组件访问元素 - Accessing an element from sibling component in Angular 2+ 如何使用列表中的选项卡动态添加元素。 角度js - How to dynamically add element with tabs in the list. Angular js 如何动态地将组件添加到另一个组件的div中? - How to dynamically add component into a div in another component? Angular 2+ 测试组件 - Angular 2+ testing a component 如何在 Angular 2+ 中将滚动元素放入视口? - How to make scroll element into viewport in Angular 2+? 如何在Angular 2+(无ngClass)中从动态创建的元素中添加和删除类? - How to add and remove classes from dynamically created elements in Angular 2+ (no ngClass)? 如何在angular 2+中为handontable添加自定义验证 - How to add custom validations for handsontable in angular 2+ Angular 2+使用“ onChange”功能在div上动态添加html文本 - Angular 2+ add dynamically html text on div with “onChange” function 如何动态设置 CSS class Angular 中的元素列表 ZA2F2ED4F8EBC2CBB1D4C21A29DC4 组件? - How can I dynamically set a CSS class list of an element in Angular from the class component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM