Angular 根据角度5中的条件禁用阵列中的元素

Angular 根据角度5中的条件禁用阵列中的元素,angular,typescript,components,Angular,Typescript,Components,我有一份与会者名单: <div class="heroWrapper"> <div class="image hero" *ngFor="let participant of participants; index as i" [class]="i === selectedParticipant ? 'selected hero' : 'image hero'"> <img [src]="participant.imageUrl" (clic

我有一份与会者名单:

<div class="heroWrapper">
    <div class="image hero" *ngFor="let participant of participants; index as i" [class]="i === selectedParticipant ? 'selected hero' : 'image hero'">
        <img [src]="participant.imageUrl" (click)="toggleMoves = !toggleMoves"/>
        <span [ngStyle]="{'color': getColor(participant)}" class="HP">{{participant.hitPoints}}</span>
        <span class="namePlayer" *ngIf="isHero(participant)">{{getPlayerName(participant)}}</span>
        <span class="nameHero">{{participant.name}}</span>
    </div>
</div>
selectedParticipant是数组中元素的索引。 现在,当参与者HP为0时,我希望他们被禁用,这意味着他们将被下一个和上一个方法跳过。当它们被禁用时,我希望它们变灰

我试着加上

if (this.participants[selectedParticipant].hitPoints = 0) {
    ++selectedParticipant;
    if (this.selectedParticipant != this.participants.length - 1) {
        ++this.selectedParticipant;
    } else {
        this.selectedParticipant = 0;
    }
}

我必须使用相同的代码两次,因为每个团队都必须对其进行检查。但出于某种原因,这将参与者的生命值设置为0?

您的if语句需要使用=而不是单个=,这就是为什么参与者的hp设置为0

=是一个比较运算符

=是赋值运算符

next() {
    if(this.participants[selectedParticipant].hitPoints == 0) {
       ++this.selectedParticipant;
       if (this.selectedParticipant > this.participants.length - 1) {
           this.selectedParticipant = 0;
       }

       this.toggleMove();
    }
}

这里发生了分配而不是比较

在左边写常量是一种习惯,这样就不会发生赋值而不是比较

例如

next() {
    if(this.participants[selectedParticipant].hitPoints == 0) {
       ++this.selectedParticipant;
       if (this.selectedParticipant > this.participants.length - 1) {
           this.selectedParticipant = 0;
       }

       this.toggleMove();
    }
}
if (0 == this.participants[selectedParticipant].hitPoints) { 
}



if (0 = this.participants[selectedParticipant].hitPoints) {
} // it would break the execution and give an error because constants cant be assigned a value and so you find out the error easily.