英文:
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.
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>injected content</ng-container>
</parent>`,
})
export class App {
name = 'Angular';
}
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">
<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">
<ng-template #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 '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<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-container slot [ngTemplateOutlet]="tpl"></ng-container>
</child>
`,
standalone: true,
imports: [CommonModule, ChildComponent],
})
export class ParentComponent {
@Input() tpl!: TemplateRef<any>;
}
@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule, ParentComponent, ChildComponent],
template: `
<parent [tpl]="content">
<div slot>injected content</div>
</parent>
<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">
<div slot>injected content</div>
</parent>
<ng-template #content>
<div slot>injected content</div>
</ng-template>
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<any>;
}
and
<child *ngFor="let item of [1,2,3]">
<ng-container slot [ngTemplateOutlet]="tpl"></ng-container>
</child>
Also, you can add de slot
atribute to the container, for filtering in the child:
child<br />
<ng-content select="[slot]"></ng-content>
<hr />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论