“ng-container” 在循环内不会重复。

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

ng-container inside a loop not repeated

问题

I'll provide the translated code portion:

对于以下的应用程序
“注入的内容”只显示在最新的子组件中,而不是显示在所有子组件中

父组件调用子组件三次,每次都注入来自AppComponent的内容。

[![点击这里输入图片描述][1]][1]

[stackblitz](https://stackblitz.com/edit/stackblitz-starters-npebzq)

```typescript
import 'zone.js/dist/zone';
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';

@Component({
  selector: 'child',
  template: `child<br /><ng-content select="[slot]"></ng-content><hr />`,
  standalone: true,
  imports: [CommonModule],
})
export class ChildComponent {}

@Component({
  selector: 'parent',
  template: `
    <child *ngFor="let item of [1,2,3]">
     <ng-content slot select="[slot]"></ng-content>
    </child>
`,
  standalone: true,
  imports: [CommonModule, ChildComponent],
})
export class ParentComponent {}

@Component({
  selector: 'my-app',
  standalone: true,
  imports: [CommonModule, ParentComponent],
  template: `
    <parent>
      <ng-container slot>注入的内容</ng-container>
    </parent>`,
})
export class App {
  name = 'Angular';
}

bootstrapApplication(App);
英文:

for the following app
the "injected content" is displayed in the latest child component only, instead of being displayed in all child components

the parent component calls the child component three times, each time it injects content that is received from AppComponent.

“ng-container” 在循环内不会重复。

stackblitz

import &#39;zone.js/dist/zone&#39;;
import { Component } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { bootstrapApplication } from &#39;@angular/platform-browser&#39;;

@Component({
  selector: &#39;child&#39;,
  template: `child&lt;br /&gt;&lt;ng-content select=&quot;[slot]&quot;&gt;&lt;/ng-content&gt;&lt;hr /&gt;`,
  standalone: true,
  imports: [CommonModule],
})
export class ChildComponent {}

@Component({
  selector: &#39;parent&#39;,
  template: `
    &lt;child *ngFor=&quot;let item of [1,2,3]&quot;&gt;
     &lt;ng-content slot select=&quot;[slot]&quot;&gt;&lt;/ng-content&gt;
    &lt;/child&gt;
`,
  standalone: true,
  imports: [CommonModule, ChildComponent],
})
export class ParentComponent {}

@Component({
  selector: &#39;my-app&#39;,
  standalone: true,
  imports: [CommonModule, ParentComponent],
  template: `
    &lt;parent&gt;
      &lt;ng-container slot&gt;injected content&lt;/ng-container&gt;
    &lt;/parent&gt;`,
})
export class App {
  name = &#39;Angular&#39;;
}

bootstrapApplication(App);

答案1

得分: 3

以下是您要翻译的内容:

You can't with ng-content, instead use ng-template and ng-container:

import 'zone.js/dist/zone';
import { Component, Input, TemplateRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';

@Component({
selector: 'child',
template: `
child


`,
standalone: true,
imports: [CommonModule],
})
export class ChildComponent {}

@Component({
selector: 'parent',
template: <child *ngFor="let item of [1,2,3]"> <ng-container slot [ngTemplateOutlet]="tpl"></ng-container> </child>,
standalone: true,
imports: [CommonModule, ChildComponent],
})
export class ParentComponent {
@Input() tpl!: TemplateRef;

}

@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule, ParentComponent, ChildComponent],
template: `
<parent [tpl]="content">

injected content
<ng-template #content>
  <div slot>injected content</div>
</ng-template>

`,

})
export class App {
name = 'Angular';
}

bootstrapApplication(App);

In the main component (in this case app), send the content you want to repeat as an input of type TemplateRef:

<parent [tpl]="content">

injected content

<ng-template #content>

injected content

Then, on your parent component (where you iterate with *ngFor), receive that content and use it within a ng-container:

export class ParentComponent {
@Input() tpl!: TemplateRef;
}

and

<child *ngFor="let item of [1,2,3]">
<ng-container slot [ngTemplateOutlet]="tpl">

Also, you can add the slot attribute to the container, for filtering in the child:

child


英文:

You can't with ng-content, instead use ng-template and ng-contatiner:

import &#39;zone.js/dist/zone&#39;;
import { Component, Input, TemplateRef } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { bootstrapApplication } from &#39;@angular/platform-browser&#39;;

@Component({
  selector: &#39;child&#39;,
  template: `
  child&lt;br /&gt;
    &lt;ng-content select=&quot;[slot]&quot;&gt;&lt;/ng-content&gt;
  &lt;hr /&gt;
  `,
  standalone: true,
  imports: [CommonModule],
})
export class ChildComponent {}

@Component({
  selector: &#39;parent&#39;,
  template: `
    &lt;child *ngFor=&quot;let item of [1,2,3]&quot;&gt;
      &lt;ng-container slot [ngTemplateOutlet]=&quot;tpl&quot;&gt;&lt;/ng-container&gt;        
    &lt;/child&gt;
`,
  standalone: true,
  imports: [CommonModule, ChildComponent],
})
export class ParentComponent {
  @Input() tpl!: TemplateRef&lt;any&gt;;

}

@Component({
  selector: &#39;my-app&#39;,
  standalone: true,
  imports: [CommonModule, ParentComponent, ChildComponent],
  template: `
    &lt;parent [tpl]=&quot;content&quot;&gt;
      &lt;div slot&gt;injected content&lt;/div&gt;
    &lt;/parent&gt;

    &lt;ng-template #content&gt;
      &lt;div slot&gt;injected content&lt;/div&gt;
    &lt;/ng-template&gt;
    
    `,
})
export class App {
  name = &#39;Angular&#39;;
}

bootstrapApplication(App);

In the main component (in this case app), send the content you want to repeat as an input of type TemplateRef:

&lt;parent [tpl]=&quot;content&quot;&gt;
      &lt;div slot&gt;injected content&lt;/div&gt;
&lt;/parent&gt;

&lt;ng-template #content&gt;
      &lt;div slot&gt;injected content&lt;/div&gt;
&lt;/ng-template&gt;

Then, on your parent componentt (wher you iterate with *ngFor), recive that content and use itt within a ng-container:

export class ParentComponent {
  @Input() tpl!: TemplateRef&lt;any&gt;;
}

and

&lt;child *ngFor=&quot;let item of [1,2,3]&quot;&gt;
      &lt;ng-container slot [ngTemplateOutlet]=&quot;tpl&quot;&gt;&lt;/ng-container&gt;        
&lt;/child&gt;

Also, you can add de slot atribute to the container, for filtering in the child:

child&lt;br /&gt;
    &lt;ng-content select=&quot;[slot]&quot;&gt;&lt;/ng-content&gt;
&lt;hr /&gt;

“ng-container” 在循环内不会重复。

huangapple
  • 本文由 发表于 2023年6月27日 20:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565000.html
匿名

发表评论

匿名网友

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

确定