In an Angular2 app, I would like to iterate over all days between startDate
and endDate
in 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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With