【问题标题】:Using the method in ngclass results Expression has changed after it was checked使用ngclass结果中的方法表达式检查后发生了变化
【发布时间】:2017-11-21 10:26:36
【问题描述】:

我正在使用在 ngfor 中循环的 ngclass 中的方法。 当循环发生时,方法返回值一直在变化。

但是我遇到了类似的错误

错误错误:ExpressionChangedAfterItHasBeenCheckedError:表达式 检查后有变化。以前的值:'background_pink'。 当前值:'background_blue'。

代码是这样的

组件.ts

getRandomColor() {
    const changedColor = _.sample(this.colors);
    return changedColor;
}

component.html

<a href="https://www.leelag.com/tag/laravel" *ngFor="let tag of bookDetails.tags">
<label [ngClass]="['label', 'well', 'well-sm', getRandomColor()]">{{ tag.name }}</label></a>

循环时一直显示上述错误。 如何解决?

【问题讨论】:

标签: angular


【解决方案1】:

您似乎正在更新循环代码中的变量/值。

*ngFor 在逻辑上循环迭代时将无法理解更新后的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 2019-01-05
    • 2019-05-29
    • 1970-01-01
    • 2018-11-17
    • 2017-01-20
    相关资源
    最近更新 更多