首页 > 解决方案 > 即使调用 ChangeDetectorRef.DetectChanges 也不会触发 Angular 7 更改检测

问题描述

我的 Angular 7 应用程序中有这段代码:

在子组件中:

@Input() lstBlocks;

在父组件中,我将此列表传递为:

<block-container [lstBlocks]="lstBlockDtos"></block-container>

lstBlockDtos是一个包含数组的数组。例如索引 0、1、2 等上的数组...

在父容器中,我正在替换其中一个嵌套数组的内容:

const dtosIndex = this.lstBlockDtos.findIndex(bDto => bDto.planDate === planDateFormatted);    
this.lstBlockDtos[dtosIndex] = mblockDtos;

...哪里mblockDtos是一个包含更多内容的新数组。

我的问题是,当我更改内部数组的内容时,更改检测对孩子不起作用。我已经实现了OnChanges生命周期方法,但它没有按预期触发。我也尝试ChangeDetectorRef.DetectChanges在更改内部数组的内容后立即调用,但更改检测仍然不起作用。

标签: angular

解决方案


推荐阅读