Created
July 1, 2020 20:04
-
-
Save nirkaufman/0beb285821882a8a11d980495ab7049f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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