【问题标题】:Angular 2: ngFor first of type without attributeAngular 2:ngFor第一个没有属性的类型
【发布时间】:2016-11-07 15:31:43
【问题描述】:

我尝试构建一个 ngFor 循环,并希望将一个类添加到第一个没有属性的元素。

这是我目前得到的:

<div *ngFor="let workout of myProgramm.trainingSchedule[myProgramm.currentWeek]; let i = index;"
             (click)="goToWorkoutDetailPage(i)"
             class="my-workout-item {{workout.workout_done ? 'is-done': ''}}">
            .....
        </div>

在这个例子中,我想将一个类添加到列表的第一个元素中,它没有属性“workout_done”。

这可能吗?

非常感谢:)

【问题讨论】:

    标签: angular


    【解决方案1】:

    你不能单独使用 *ngFor 来做到这一点。 *ngFor中的其他项目无法引用。

    最好找到第一个没有workout_done的元素,将其赋值给类中的一个属性firstUndoneWorkout,然后使用

        <div *ngFor="let workout of myProgramm.trainingSchedule[myProgramm.currentWeek]; let i = index;"
             (click)="goToWorkoutDetailPage(i)"
             class="my-workout-item" [class.workout_done]="workout == firstUndoneWorkout">
            .....
        </div>
    

    【讨论】:

      【解决方案2】:

      你很亲密,正确的做法是:

      [class.is-done]="workout.workout_done"

      如果你想用没有锻炼的人来做,只需检查相反的。

      这里是模板语法文档:https://angular.io/docs/ts/latest/guide/template-syntax.html#!#ngClass

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-01
        • 2015-08-09
        • 2017-04-04
        • 2021-05-06
        • 1970-01-01
        • 2017-07-12
        相关资源
        最近更新 更多