在Angular2组件的HTML模板中实现计数器

时间:2017-04-09 12:08:17

标签: html angular

我正在尝试构建自定义日历。还有很多事情要做,但我在这一点上的问题是如何在Angular2组件的HTML模板中实现一个计数器。更具体地说:

<div class="container">
  <table class="table table-bordered">
    <tbody>
        <tr *ngFor="let w of weeks">
            <td *ngFor="let d of days">
                <ng-container *ngIf="(day_count <= monthLength && (w > 1 || d >= firstDayOfMonth))">
                    {{ day_count }}
                    // On this point, I want "day_count = day_count + 1"...
                </ng-container>
            </td>
        </tr>
    </tbody>
 </table>
</div>

关于上述变数:

 weeks = [1,2,3,4,5,6];
 days = [0,1,2,3,4,5,6];
 monthLength = 28 or 29 or 30 or 31
 firstDayOfMonth = from 0 to 6

变量“day_count”是负责显示每月日期的变量。最初,它必须是1,然后增加1等,直到达到月长。那么,我该如何实现这个呢?

1 个答案:

答案 0 :(得分:0)

你不能没有模板方面,

但我认为您的代码所需的解决方案是:

模板面:

<div class="container">
  <table class="table table-bordered">
    <tbody>
        <tr *ngFor="let w of weeks">
            <td *ngFor="let d of days">
                <ng-container *ngIf="incr(w,d,monthLength,firstDayOfMonth)">
                    {{ incr(w,d,monthLength,firstDayOfMonth) }}
                </ng-container>
            </td>
        </tr>
    </tbody>
 </table>
</div>

组件方:

weeks = [1,2,3,4,5,6];
days = [0,1,2,3,4,5,6];
firstDayOfMonth = 0;
monthLength = 31;

incr(w,d,monthLength,firstDayOfMonth)
{
    let day_count = ((w-1)*7)+d;
    if (day_count < (monthLength+firstDayOfMonth)  && day_count >= firstDayOfMonth)
    {
        return day_count - firstDayOfMonth+1;
    }
}