rating = 4;
for(i=0; i < rating ; i++){
//print statement
}
how to replicate the same for loop with conditions in angular 6 using *ngFor
th
The best solution I think is using a directive like @pdudits say in the link. To improve the directive I propouse
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[repeat]'
})
export class RepeatDirective {
constructor(
private templateRef: TemplateRef,
private viewContainer: ViewContainerRef) { }
@Input() set repeat(times: number) {
let count=this.viewContainer.length;
for (let i=this.viewContainer.length;i>times;i--)
this.viewContainer.remove(i-1);
for (let i = count ; i < times ; i++)
this.viewContainer.createEmbeddedView(this.templateRef,
{
$implicit:i
});
}
}
You can use as
{{index}}
See stackblitz