如何切割queryList并在Angular中显示新数组(新的QueryList)。

huangapple go评论71阅读模式
英文:

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:

&lt;ng-template&gt;
  &lt;ng-content&gt;&lt;/ng-content&gt;
&lt;/ng-template&gt;

and in avatar-list.component.ts:

 @ContentChildren(ThyAvatarComponent)
    private set avatarComponents(value: QueryList&lt;ThyAvatarComponent&gt;) {
        this.avatarItems = value.toArray();
        this.avatarList = value;
    }

I use avatar-list like this:

&lt;thy-avatar-list&gt;
  &lt;thy-avatar-component *showDetailTip &gt;&lt;/thy-avatar-component&gt;
  &lt;thy-avatar-component *showDetailTip &gt;&lt;/thy-avatar-component&gt;
&lt;/thy-avatar-list&gt;

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:

&lt;div *ngFor=&quot;let item of avatarItems | slice : 0 : showCount&quot;&gt;
  &lt;!--? --&gt;
&lt;/div&gt;

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

as needed.

Also, ensure that the is being used correctly as the parent container for the content projection () 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';
}
英文:

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

&lt;div *ngFor=&quot;let item of avatarItems.slice(0, showCount)&quot;&gt;
  &lt;!-- Display item properties or use them within the &lt;ng-content&gt; --&gt;
  &lt;p&gt;{{ item.someProperty }}&lt;/p&gt;
  &lt;ng-content&gt;&lt;/ng-content&gt;
&lt;/div&gt;

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&lt;ThyAvatarComponent&gt;) {
  this.avatarItems = value.toArray();
  this.avatarList = value;
  this.slicedAvatarItems = this.avatarItems.slice(0, this.showCount);
}

html

&lt;ng-container *ngFor=&quot;let item of slicedAvatarItems&quot;&gt;
  &lt;ng-template [ngTemplateOutlet]=&quot;item.templateRef&quot;&gt;&lt;/ng-template&gt;
&lt;/ng-container&gt;

**get templateRef in ThyAvatarComponent**

Ts

import { ViewChild } from &#39;@angular/core&#39;;

@ContentChildren(ThyAvatarComponent)
@ViewChild(ThyAvatarComponent, { read: TemplateRef }) avatarComponents: QueryList&lt;ThyAvatarComponent&gt;;

HTML

&lt;ng-container *ngFor=&quot;let item of avatarComponents&quot;&gt;
  &lt;ng-template [ngTemplateOutlet]=&quot;item.templateRef&quot;&gt;&lt;/ng-template&gt;
&lt;/ng-container&gt;

**Updated answer**

avatar-list.component.html:

&lt;ng-content&gt;&lt;/ng-content&gt;

avatar-list.component.ts:

import { Component, ContentChildren, QueryList, AfterContentInit, ElementRef } from &#39;@angular/core&#39;;
import { ThyAvatarComponent } from &#39;./thy-avatar.component&#39;;

@Component({
  selector: &#39;thy-avatar-list&#39;,
  template: `
    &lt;ng-container *ngFor=&quot;let item of templateRefs&quot;&gt;
      &lt;ng-container *ngTemplateOutlet=&quot;item&quot;&gt;&lt;/ng-container&gt;
    &lt;/ng-container&gt;
  `,
})
export class ThyAvatarListComponent implements AfterContentInit {
  @ContentChildren(ThyAvatarComponent, { read: ElementRef }) avatarComponents: QueryList&lt;ElementRef&gt;;

  templateRefs: Array&lt;ElementRef&lt;HTMLElement&gt;&gt; = [];

  ngAfterContentInit() {
    this.avatarComponents.forEach((avatarComponent) =&gt; {
      this.templateRefs.push(avatarComponent.nativeElement);
    });
  }
}

thy-avatar.component.ts:

import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;thy-avatar-component&#39;,
  template: `
    &lt;ng-template #avatarTemplate&gt;
      &lt;!-- Your avatar component template here --&gt;
      &lt;div&gt;{{ avatarData }}&lt;/div&gt;
    &lt;/ng-template&gt;
  `,
})
export class ThyAvatarComponent {
  avatarData = &#39;Avatar Data&#39;;
}

huangapple
  • 本文由 发表于 2023年5月11日 10:29:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定