Skip to content

Instantly share code, notes, and snippets.

@PedroPovedaQ
Created January 5, 2021 15:33
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 PedroPovedaQ/5488fcf5298923639f0fb0515bcc3a87 to your computer and use it in GitHub Desktop.
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)
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