Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 generator function

In an Angular2 app, I would like to iterate over all days between startDate and endDatein a template. Something like this:

<div *ngFor="let date of dateRange(startDate,endDate)">
    {{date}}
</div> 

I realize that I could precalculate all these dates and store them in an array before displaying them, but I would rather not take up the memory. I have been reading up on javascript generator functions and it seems like in vanilla javascript, I could do this:

function* dateRange(start,end) { 
    let cur = start;
    while (cur <= end) {
        yield cur;
        cur.setDate(cur.getDate() + 1);
    }
}

Is there any way to use a generator function like this from within an Angular2 component which could be used in a template iterator?

like image 671
spectacularbob Avatar asked Nov 20 '22 15:11

spectacularbob


1 Answers

that I could precalculate all these dates and store them in an array before displaying them, but I would rather not take up the memory

Any memory will be much much less than the memory used by the DOM with all those dom elements.

The only solution I can think of is making an array out of the generator before using ngFor and at that point why no just only use an array.

like image 165
basarat Avatar answered Jan 05 '23 06:01

basarat