简体   繁体   English

更改动态颜色 - ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改

[英]change dynamic color - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

I have json like:我有 json 像:

export class NestedTag {
  code: number;
  name: string;
  parent: number;
  children?: NestedTag[];  
}

I want to display each tag and its children in a circle in a different color.我想以不同颜色将每个标签及其子项显示在一个圆圈中。

To do this: There is an array of colors:为此:有一个 colors 数组:

colorTagsList= new Array<string>('#FFA200','#26BCC0','#475363','#6BB745','#0D7BAF');

Function for obtaining the current color: Function 获取当前颜色:

getColor( ):string{
 if(this.colorTagsList[this.indexColor] ===undefined){
  this.indexColor=0;
 }
  return this.colorTagsList[this.indexColor++];
}

my HTML code:我的 HTML 代码:

<div > 
            <span *ngFor="let tags of nestedRefTags; Last as lastTag" >{{lastTag}}
              <button class="tag" [ngStyle]="{'background-color': getColor()}">
                 <span > {{ tags.parent.name}}</span>
                 <span *ngIf="tags.children.length>0" > -</span>
                 <span *ngFor="let tag of tags.children ; last as isLast">
                  <span> {{tag.name}}</span>
                  <span *ngIf="!isLast">  |  </span>
                </span>
              </button>
            </span>
        </div> 

But I get an error:但我得到一个错误:

core.js:6237 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '#FFA200'. Current value: '#0D7BAF'.
    at throwErrorIfNoChangesMode (core.js:8156)
    at bindingUpdated (core.js:20051)
    at pureFunction1Internal (core.js:36876)
    at Module.ɵɵpureFunction1 (core.js:36656)
    at ViewItemComponent_span_29_Template (view-item.component.html:45)
    at executeTemplate (core.js:12098)
    at refreshView (core.js:11945)
    at refreshDynamicEmbeddedViews (core.js:13335)
    at refreshView (core.js:11968)
    at refreshComponent (core.js:13410)

What should I do?我应该怎么办?

getColor() is a function and will be reevaluated everytime a change is detected. getColor()是一个 function 并且每次检测到更改时都会重新评估。 Every time you change the value of this.indexColor this function is reeveluated and a new color returned.每次更改this.indexColor的值时,都会重新调整此 function 并返回新颜色。 At the end of the reevaluation you will end up with all your tags having the same color.在重新评估结束时,您最终会得到所有标签的颜色相同。

Below solution removes the getColor() and assigns each tag a color property that is then used to set the color下面的解决方案删除getColor()并为每个标签分配一个颜色属性,然后用于设置颜色

 colorTagsList = new Array<string>(
    "#FFA200",
    "#26BCC0"
    ...
  );
  nestedRefTags = [ ... ].map((item, i) => ({...item, color: this.colorTagsList[i]}) );

and in the html并在 html

<div>
  <span *ngFor="let tags of nestedRefTags; last as lastTag"
    >{{lastTag}}
    <button class="tag" [ngStyle]="{'background-color': tags.color }">
      <span> {{ tags.parent.name}}</span>
      <span *ngIf="tags.children.length > 0"> -</span>
      <span *ngFor="let tag of tags.children ; last as isLast">
        <span> {{tag.name}}</span>
        <span *ngIf="!isLast"> | </span>
      </span>
    </button>
  </span>
</div>

See this demo 看这个演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ngu-carousel :: :: ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改 - ngu-carousel :: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked 共享服务:ExpressionChangedAfterItHasBeenCheckedError:检查表达式后,表达式已更改 - Shared service: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked ExpressionChangedAfterItHasBeenCheckedError:使用Angular2检查表达式后,表达式已更改 - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked using Angular2 ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 先前的值:&#39;ngIf:false&#39;。 当前值:&#39;ngIf:true&#39; - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: false'. Current value: 'ngIf: true' ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。 以前的值:&#39;ngIf:true&#39;。 当前值:&#39;ngIf:false&#39; - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: true'. Current value: 'ngIf: false' ExpressionChangedAfterItHasBeenCheckedError:从ckEditor获取值时检查后表达式已更改 - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked while getting the value from ckEditor 离子2:检查后错误表达已更改 - Ionic 2: Error Expression has changed after it was checked 角度4错误:检查后表达式已更改 - Angular 4 Error: Expression has changed after it was checked “检查后表达式已更改”的控制台错误 - console error for “Expression has changed after it was checked” Angular - 表达式在检查后发生变化 - Angular - Expression Changed After It Has Been Checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM