Created
January 5, 2021 15:33
-
-
Save PedroPovedaQ/5488fcf5298923639f0fb0515bcc3a87 to your computer and use it in GitHub Desktop.
Angular Directive to Repeat without *ngFor (Useful when wanting to print multiple times without array)
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 { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; | |
@Directive({ selector: '[ngRepeat]' }) | |
export class RepeatDirective { | |
constructor( | |
private templateRef: TemplateRef<any>, | |
private viewContainerRef: ViewContainerRef | |
) {} | |
@Input() set ngRepeat(iterations: number) { | |
this.viewContainerRef.clear(); | |
for (let index = 0; index < iterations; ++index) { | |
this.viewContainerRef.createEmbeddedView(this.templateRef); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment