MatExpansionPanel表达式在检查错误后已更改'mat-expanded:true' [英] MatExpansionPanel expression has changed after it was checked error 'mat-expanded: true'
问题描述
我有一个Angular Material Expansion Panel查询列表:
I have an Angular Material Expansion Panel Query list:
@ViewChildren(MatExpansionPanel)
matExpansionPanelQueryList: QueryList<MatExpansionPanel>;
和一个简单的数组:
questions: [];
扩展面板是用*ngFor
生成的:例如,简化:
The Expansion Panels are generated with an *ngFor
: simplified for example:
<ng-container *ngFor="let question of questions>
<mat-expansion-panel
...
扩展问题数组时,我要打开最后一个扩展面板.
When I extend the questions array, I want to open up the last expansion panel.
extendQuestion() {
this.matExpansionPanelQueryList.changes.subscribe(
change => {
change.last.open();
}
);
this.questions.push('some-new-item');
}
这工作得很好-我将一个项目插入数组,重新渲染ExpansionPanels,创建了一个新的Panel,它实际上打开了-我的问题是它在控制台中生成以下错误:
This works just fine - I insert an item to the array,
the ExpansionPanels get's re-rendered, a new Panel gets created and it actually opens up - my problem is that it generates the following error in the console:
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has
changed after it was checked. Previous value: 'mat-expanded: false'. Current
value: 'mat-expanded: true'.
有什么办法可以避免这种情况?
我已经尝试在订阅中使用changeDetectorRef
和markForCheck()
,但是错误消息并没有消失(老实说,我100%确信这里的确切问题是什么).
Is there any way to avoid this?
I have tried using changeDetectorRef
and markForCheck()
in the subscription, but the error message did not go away (and honestly, I am 100% sure what's the exact problem here).