表达式在检查了Angular隐藏指令触发的错误之后发生了变化



我遇到了一个错误:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.

html

<div #visible >
    <div [hidden]="toInt(visible.style.width)<19"  >
    </div>
</div>

.ts

 toInt(x:string):number{
    return parseInt(x); 
  }

通常的药物是在变化后明确运行变更检测。但是我没有更改.ts文件中的任何属性,所以我不知道要放在哪里

this.changeDetectorRef.detectChanges();

我该怎么办?

我在不同的情况下经历了痛苦。可能的修复是

  1. .ts @ViewChild('visible') divElement: ElementRef;
  2. 中的访问#Visible
  3. 和.ts

     ngAfterViewInit() {
           this._hideDiv= '<get the div length and do your condition>...'   
    

    }

  4. 最后在您的模板中<div [hidden]="_hideDiv"> </div>

相关内容

最新更新