Angular ng-class 三元没有随着模型的变化更新我的模板

Angular ng-class ternary doesn't update my template as the model changes

当我的页面最初加载时,它使用此 ng-class 三元运算符正确设置 class:

<div ng-class="{{day.current}} ? 'currPeriod' : 'notCurrPeriod'">
    {{day.date | date:"d"}}
</div>

注意 - 此 html 在模板中,由指令使用,在 ng-repeat

中生成

日期 {{day.date | date:"d"}} 更新,但 class 未应用。

奇怪的是,我的开发工具显示出差异:

1 = true 所以它的计算结果应该是 currPeriod 但元素 class 仍然是 notCurrPeriod。我在模式更改结束时尝试了 $scope.$apply(),但这没有用。 如何让模板重新运行并应用此三元组?或者有更好的方法吗?

<div ng-class="{'currPeriod': day.current, 'notCurrPeriod': !day.current}">
  {{day.date | date:"d"}}
</div>

ng-class 不能这样工作,使用对象语法来实现你想要的,或者你可以在日期不在当前期间时设置默认样式,然后应用class 如果是,它会缩短你的 ng-class 语句