Skip to content

Instantly share code, notes, and snippets.

@nirkaufman
Created July 1, 2020 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirkaufman/0beb285821882a8a11d980495ab7049f to your computer and use it in GitHub Desktop.
Save nirkaufman/0beb285821882a8a11d980495ab7049f to your computer and use it in GitHub Desktop.
import {AfterViewInit, Component, QueryList, ViewChildren} from '@angular/core';
import {NgComponentOutlet} from "@angular/common";
// Something to loop over
@Component({
template: `<p>Component Type A</p>`
})
class ComponentA {}
@Component({
template: `<p>Component Type B</p>`
})
class ComponentB {}
@Component({
template: `<p>Component Type C</p>`
})
class ComponentC {}
@Component({
selector: 'my-app',
template: `
<h1>Ideas</h1>
<ng-container *ngFor="let comp of components"
[ngComponentOutlet]="comp">
</ng-container>
`,
})
export class AppComponent implements AfterViewInit{
components = [ComponentA, ComponentA, ComponentA, ComponentB, ComponentC];
@ViewChildren(NgComponentOutlet) viewComponent: QueryList<any>;
ngAfterViewInit() {
const justTypeB = this.viewComponent.filter( (c:NgComponentOutlet, index: number) => {
// filter component of type Component B, and even index
return (c.ngComponentOutlet === ComponentA) && (index % 2 === 0);
})
console.log(justTypeB);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment