简体   繁体   English

角度变化检测

[英]Angular change detection

I have 2 components.我有 2 个组件。 component A and component B. In component A has an output that when emit set an input in component B when this happens I get this error:组件 A 和组件 B。在组件 A 中有一个输出,当发生这种情况时,在组件 B 中设置一个输入时,我收到此错误:

expression has been changed after it was checked.表达式在检查后已更改。

The components are shown below:组件如下图所示:

<app-cmp-A (onSelect)="selectedData = $event"></app-cmp-A>
<app-cmp-B [selectedItem]="selectedData"></app-cmp-B>

My question is that why this error happen and this scenario is very useful when we use cascading component that one component require output another?我的问题是为什么会发生此错误,并且当我们使用一个组件需要输出另一个组件的级联组件时,这种情况非常有用?

This will not be effective if you are not loading both the components together!如果您没有同时加载两个组件,这将无效! If you cannot still detect it with the above solution, you can use subject and pass the value inside the subject!如果使用上述解决方案仍然无法检测到它,则可以使用主题并传递主题内部的值! So it can be subscribed in the other component and the data can be sent!所以它可以在其他组件中订阅并发送数据!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM