Skip to content

Instantly share code, notes, and snippets.

@Burgov
Created August 9, 2022 17:25
Show Gist options
  • Save Burgov/20de8b23ead2a5f3c53ed8db4731b155 to your computer and use it in GitHub Desktop.
Save Burgov/20de8b23ead2a5f3c53ed8db4731b155 to your computer and use it in GitHub Desktop.
@Component({
selector: 'content-component',
template: '<div>Test {{ data }}</div>',
})
export class ContentComponent {
@Input() data: string;
}
@Component({
selector: 'child-component',
template: '<div>I have {{ childCount }} content children! Data: {{ childData|json }}</div><div><ng-content></ng-content></div>',
})
export class ChildComponent implements AfterContentInit {
@ContentChildren(ContentComponent) children: QueryList<ContentComponent>;
childCount: number;
childData: string[];
ngAfterContentInit() {
this.childCount = this.children.length;
this.childData = this.children.map(child => child.data);
}
}
@Component({
selector: 'parent-component',
template: '<child-component><content-component *ngFor="let el of elements" [data]="el"></content-component></child-component>',
})
export class ParentComponent {
elements = ['a', 'b', 'c', 'd', 'e'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment