Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 表达式更改TerrithasBeenCheckedError解决方案_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 表达式更改TerrithasBeenCheckedError解决方案

Javascript 表达式更改TerrithasBeenCheckedError解决方案,javascript,angular,typescript,Javascript,Angular,Typescript,我正在实现以下*ngIf。我知道从技术上讲,我不应该从*ngIf中调用函数,但黑客需要它: <div *ngIf="!ddlAuthorsSelect2Bound && wireUpSelect2()"></div> 您知道抑制此错误的方法吗?首先避免使用*ngIf wireUpSelect2()中的函数 检查err表达式后,其原因已更改。上一个值:“true”。当前值:“false”。表示模板解析按顺序进行。如果布尔值!ddlAuthorsSelect

我正在实现以下*ngIf。我知道从技术上讲,我不应该从*ngIf中调用函数,但黑客需要它:

<div *ngIf="!ddlAuthorsSelect2Bound && wireUpSelect2()"></div>

您知道抑制此错误的方法吗?

首先避免使用*ngIf wireUpSelect2()中的函数


检查err
表达式后,其原因已更改。上一个值:“true”。当前值:“false”。
表示模板解析按顺序进行。如果布尔值
!ddlAuthorsSelect2Bound
在html的后续行中被更改,您在html的顶部调用此布尔值。出现此错误。

快速而肮脏的解决方法是将其放入组件:
public ngDoCheck():void{this.cdRef.detectChanges();}

确保从“@angular/core”导入:
import{…,ChangeDetectorRef}

并将其包含在构造函数中:
constructor(…,private-cdRef:ChangeDetectorRef){…}


是@angular 4.2.x的问题。无论它是否是最佳实践,解决方案都比添加一个生命周期检查要麻烦得多。如果我没有弄错的话,您不必为4.3.x执行此操作。

谢谢我知道原因,但要求解决方法如果您可以共享html和ts文件,我会找到一些解决方法。一般意义上的解决方法是使用不同的布尔变量,并在后续顺序中的其他布尔变量的帮助下更改一个布尔变量的值。我不确定是否理解,DDLAuthorselect2Bond是一个“开关”,用于在wireUpSelect2()时检测调用或ngIf何时生效?嗨,织女星-对不起,我不清楚。ddlAuthorsSelect2Bound的默认值为false,但在调用wireUpSelect2()后设置为true。由于*ngIf表达式被多次实现,wireUpSelect2()有一个保护子句,如果DDAuthorsSelect2Bound已设置为true,则该子句将跳过wireup逻辑。在wireUpSelect2()中检查DDAuthorsSelect2Bound值如何?例如,如果(ddlauthorselect2bound)返回。有点“半解决方法”。@vega-yes我正在检查方法内部,但我希望通过简单的变量检查避免方法调用altogther。但我认为你是对的,在方法中检查它将是最干净的解决方法。谢谢
<div *ngIf="wireUpSelect2()"></div>
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.