当我尝试迭代 ngFor 循环时,Angular 2+ attr.disabled 不适用于 div

标签 angular typescript ngfor

我正在开发一个约会预订应用程序,我在其中使用 *ngFor 循环显示约会的时间段。

html

<div *ngFor="let item of allTimeSlots">
    <div class="col-sm-3">
        <div class="choice" data-toggle="wizard-slot" [attr.disabled]="item.status" (click)="slotSelected($event)">
            <input type="radio" name="slot" value="{{item.timeSlot}}">
            <span class="icon">{{item.timeSlot}}</span> {{item.status}}
        </div>
    </div>
</div>

typescript

for (var index = 0; index < this.totalMinutes; index += 15, i++) {
  this.allTimeSlots[i] = new allTimeSlots("", false);

  this.allTimeSlots[i].timeSlot = (hour <= 12 ? hour : hour - 12) + ":" + (minute <= 9 ? ("0" + minute) : minute) + " " + ampm;
  this.bookedTimeSlots.forEach(element => {
    if (this.allTimeSlots[i].timeSlot == element) {
      this.allTimeSlots[i].status = true;
    }
  });
}

这是时间段的屏幕截图,如果时间段已预订,则显示 true,如果可用于调试目的,则显示 false。 enter image description here

当我运行这段代码时,它没有抛出任何错误,但是 *ngFor 创建的所有 div 元素都被禁用了。我尝试使用 *ngIf 而不是禁用,它工作得很好。但问题是我想显示时间段是否可用。

最佳答案

这样使用:

[attr.disabled]="isDisabled ? '' : null"

同样适用于只读。

关于当我尝试迭代 ngFor 循环时,Angular 2+ attr.disabled 不适用于 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45256076/

相关文章:

angular - 如何将 Angular 4 Web 应用程序连接到移动摄像头?

typescript - Bootstrap Vue 阻止/取消事件

javascript - NG 构建失败模块解析失败 : Unexpected token - nothing has changed

javascript - Angular 2 编写像 ngFor、iterableDiffer 这样的结构指令可能会导致错误

html - 使用 ngfor 将点击事件添加到迭代列表

在 Windows 上使用 WebStorm+Chrome 调试 Angularjs2+npm

html - 如何在 Angular 4 中使用 renderer2 动态追加子项

html - 如何创建带有 Angular 数组的表行?

angular - 如何在 Angular 6 中将 HTML 标签或元素附加到 div 中?

javascript - 类之间的 TypeScript 回调