I have a parent component that displays a list of validation errors and displays a list of child components with ngFor
. Each child component performs validation during ngOnInit
and outputs the result to the parent component. The parent component listens for this output and updates the list of validation errors, the result of which causes an error:
Expression has changed after it was checked
Now I understand why this error occurs - at the beginning of the change detection cycle, the verification errors were in one state, and at the end they were in a different state, and this is not allowed.
I do not understand how to get around this problem. The parent component should display a list of errors at the top of the page, and each child component will add validation results to this list. If this is a violation of the unidirectional data stream, please tell me how to get around this in a clean way (i.e. do not wrap the check in setTimeout
without changing from an immutable list to a mutable one, rather than explicitly invoking the change detector again after checking).
Plunker reproduces the problem: https://plnkr.co/edit/q52A1DraNOnxZa0qGFDo?p=preview
EDIT
I "solved" the problem by building an EventEmitter
with the isAsync
flag:
new EventEmitter(true)
This means that the values will be emitted asynchronously, so the outgoing values will be selected in the next change detection cycle. I think the result is the same as porting the logic to setTimeout
, but in this way, at least we don’t need to port our code to a timeout wherever we can emit a value.
source share