英文:
how to slice queryList and show the new array(new QueryList) in angular
问题
Sure, here's the translated code part:
在我的 avatar-list.html 文件中:
<ng-template>
<ng-content></ng-content>
</ng-template>
而在 avatar-list.component.ts 文件中:
@ContentChildren(ThyAvatarComponent)
private set avatarComponents(value: QueryList<ThyAvatarComponent>) {
this.avatarItems = value.toArray();
this.avatarList = value;
}
我像这样使用 avatar-list:
<thy-avatar-list>
<thy-avatar-component *showDetailTip></thy-avatar-component>
<thy-avatar-component *showDetailTip></thy-avatar-component>
</thy-avatar-list>
我为 thyAvatarComponent 添加了一些指令。现在,我想在 HTML 中对 this.avatarList 进行切片,显示其中的一部分,应该怎么做?
我尝试在 avatar-list.html 中使用 slice 管道:
<div *ngFor="let item of avatarItems | slice: 0: showCount">
<!--? -->
</div>
但我不知道如何在第二行中显示 item(带有指令的 avatarComponent)?
英文:
in my avatar-list.html:
<ng-template>
<ng-content></ng-content>
</ng-template>
and in avatar-list.component.ts:
@ContentChildren(ThyAvatarComponent)
private set avatarComponents(value: QueryList<ThyAvatarComponent>) {
this.avatarItems = value.toArray();
this.avatarList = value;
}
I use avatar-list like this:
<thy-avatar-list>
<thy-avatar-component *showDetailTip ></thy-avatar-component>
<thy-avatar-component *showDetailTip ></thy-avatar-component>
</thy-avatar-list>
I add some directive for thyAvatarComponent.
Now,I want to slice this.avatarList,show part of this.avatarList in html, how should I do?
I try to use slicePipe in avatar-list.html:
<div *ngFor="let item of avatarItems | slice : 0 : showCount">
<!--? -->
</div>
but I don't know how to show the itme(avatarComponent with directive) in line 2?
答案1
得分: 1
以下是您要翻译的内容:
"To access the individual items of avatarItems in your HTML template, you can use the ngFor directive and iterate over avatarItems directly.
Here's an example:
HTML
<div *ngFor="let item of avatarItems.slice(0, showCount)">
<!-- Display item properties or use them within the <ng-content> -->
<p>{{ item.someProperty }}</p>
<ng-content></ng-content>
</div>
slice method is called directly on avatarItems within the ngFor directive, allowing you to display only the desired number of items based on the showCount variable.
Make sure to replace someProperty with the actual property name you want to display from item. You can access any property of item inside the
Also, ensure that the
**Updated Answer**
In your avatar-list.component.ts file, add a public property to hold the sliced avatarItems:
public slicedAvatarItems: ThyAvatarComponent[];
@ContentChildren(ThyAvatarComponent)
private set avatarComponents(value: QueryList<ThyAvatarComponent>) {
this.avatarItems = value.toArray();
this.avatarList = value;
this.slicedAvatarItems = this.avatarItems.slice(0, this.showCount);
}
html
<ng-container *ngFor="let item of slicedAvatarItems">
<ng-template [ngTemplateOutlet]="item.templateRef"></ng-template>
</ng-container>
**get templateRef in ThyAvatarComponent**
Ts
import { ViewChild } from '@angular/core';
@ContentChildren(ThyAvatarComponent)
@ViewChild(ThyAvatarComponent, { read: TemplateRef }) avatarComponents: QueryList<ThyAvatarComponent>;
HTML
<ng-container *ngFor="let item of avatarComponents">
<ng-template [ngTemplateOutlet]="item.templateRef"></ng-template>
</ng-container>
**Updated answer**
avatar-list.component.html:
<ng-content></ng-content>
avatar-list.component.ts:
import { Component, ContentChildren, QueryList, AfterContentInit, ElementRef } from '@angular/core';
import { ThyAvatarComponent } from './thy-avatar.component';
@Component({
selector: 'thy-avatar-list',
template: `
<ng-container *ngFor="let item of templateRefs">
<ng-container *ngTemplateOutlet="item"></ng-container>
</ng-container>
`,
})
export class ThyAvatarListComponent implements AfterContentInit {
@ContentChildren(ThyAvatarComponent, { read: ElementRef }) avatarComponents: QueryList<ElementRef>;
templateRefs: Array<ElementRef<HTMLElement>> = [];
ngAfterContentInit() {
this.avatarComponents.forEach((avatarComponent) => {
this.templateRefs.push(avatarComponent.nativeElement);
});
}
}
thy-avatar.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'thy-avatar-component',
template: `
<ng-template #avatarTemplate>
<!-- Your avatar component template here -->
<div>{{ avatarData }}</div>
</ng-template>
`,
})
export class ThyAvatarComponent {
avatarData = 'Avatar Data';
}
英文:
To access the individual items of avatarItems in your HTML template, you can use the ngFor directive and iterate over avatarItems directly.
Here's an example:
HTML
<div *ngFor="let item of avatarItems.slice(0, showCount)">
<!-- Display item properties or use them within the <ng-content> -->
<p>{{ item.someProperty }}</p>
<ng-content></ng-content>
</div>
slice method is called directly on avatarItems within the ngFor directive, allowing you to display only the desired number of items based on the showCount variable.
Make sure to replace someProperty with the actual property name you want to display from item. You can access any property of item inside the <div> as needed.
Also, ensure that the <ng-template> is being used correctly as the parent container for the content projection (<ng-content>) in the consuming component
**Updated Answer**
In your avatar-list.component.ts file, add a public property to hold the sliced avatarItems:
public slicedAvatarItems: ThyAvatarComponent[];
@ContentChildren(ThyAvatarComponent)
private set avatarComponents(value: QueryList<ThyAvatarComponent>) {
this.avatarItems = value.toArray();
this.avatarList = value;
this.slicedAvatarItems = this.avatarItems.slice(0, this.showCount);
}
html
<ng-container *ngFor="let item of slicedAvatarItems">
<ng-template [ngTemplateOutlet]="item.templateRef"></ng-template>
</ng-container>
**get templateRef in ThyAvatarComponent**
Ts
import { ViewChild } from '@angular/core';
@ContentChildren(ThyAvatarComponent)
@ViewChild(ThyAvatarComponent, { read: TemplateRef }) avatarComponents: QueryList<ThyAvatarComponent>;
HTML
<ng-container *ngFor="let item of avatarComponents">
<ng-template [ngTemplateOutlet]="item.templateRef"></ng-template>
</ng-container>
**Updated answer**
avatar-list.component.html:
<ng-content></ng-content>
avatar-list.component.ts:
import { Component, ContentChildren, QueryList, AfterContentInit, ElementRef } from '@angular/core';
import { ThyAvatarComponent } from './thy-avatar.component';
@Component({
selector: 'thy-avatar-list',
template: `
<ng-container *ngFor="let item of templateRefs">
<ng-container *ngTemplateOutlet="item"></ng-container>
</ng-container>
`,
})
export class ThyAvatarListComponent implements AfterContentInit {
@ContentChildren(ThyAvatarComponent, { read: ElementRef }) avatarComponents: QueryList<ElementRef>;
templateRefs: Array<ElementRef<HTMLElement>> = [];
ngAfterContentInit() {
this.avatarComponents.forEach((avatarComponent) => {
this.templateRefs.push(avatarComponent.nativeElement);
});
}
}
thy-avatar.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'thy-avatar-component',
template: `
<ng-template #avatarTemplate>
<!-- Your avatar component template here -->
<div>{{ avatarData }}</div>
</ng-template>
`,
})
export class ThyAvatarComponent {
avatarData = 'Avatar Data';
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论