*除了最后2个Angular 4之外的所有列的Ngif

时间:2017-11-06 12:27:57

标签: angular ngif

我尝试使用* Ngif用于除了最后2个之外的所有列,我已经尝试了几个小时,但到目前为止没有工作 mes变量具有当前月份的数量。 listaMonthsNames与当前月份具有相同数量的元素 check变量是布尔值设置为false

<thead>
    <tr>
      <th></th>
      <ng-container *ngFor="let month of listaMonthsNames let idx=index">
      <th class="col-{{idx}}" *ngIf="check">{{month}}</th>
      </ng-container>
    </tr>
</thead>

当我点击它时,我也有一个复选框显示数组listaMonthsNames的所有月份,如果我再次点击它会隐藏所有月份,但我希望它可以留下最后2个月可见

2 个答案:

答案 0 :(得分:1)

如果您尝试应用ngFor来排除列,则可以使用切片管道:

<....*ngFor="let month of listaMonthsNames | slice: mySlicer"...>

其中mySlicer可以包含0的值 - 包括所有 - 至listaMonthsNames.length - 排除N区间内的所有号码和[0;listaMonthsNames.length]个号码以排除N个列,例如:2 - 排除前两列,如此demo example

答案 1 :(得分:0)

试试这个,这只会显示listaMonthsNames

的最后一个值
<thead>
    <tr>
      <th></th>
      <ng-container *ngFor="let month of listaMonthsNames let idx=index">
      <th class="col-{{idx}}" *ngIf="(idx == listaMonthsNames.length || idx == (listaMonthsNames.length-1))">{{month}}</th>
      </ng-container>
    </tr>
</thead>