Virtual Scroll template caching breaking behavior for Components with ngOnInit #16330
Labels
area: cdk/scrolling
P3
An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
It seems that virtual scroll is not rerunning ngOnInit of template components when template views are being cached. This results in having invalid data if the component performs initial functionality on onInit.
Note that the problem below does not happen if template caching is disabled with
templateCacheSize: 0
.I've replicated the issue on this stackblitz: https://stackblitz.com/edit/virtual-scroll-pointer-bug
I have the following scenario where I have a component; in the example,
PersonComponent
; which is repeated for each element of an array within a virtual scroll vieport (seeapp.component.html
). ThePersonComponent
takes aPerson
object as an input, and onInit retrieves anActivity
object based on some activity ID from an array of Activities that is cached on a service (ActivityService
). The retrieved object is then assigned on a variable within thePersonComponent
class.The problem relies when the end user scrolls down and up in the virtual scroll container. When the elements are destroyed and recreated, it seems that the pointers of the activity variable on the
PersonComponent
is set to point to another incorrect value.To replicate the problem,
Note that the content of the activity array is not changing, so it's not an issue of some other process changing the content of the object pointed to. Also if you scroll on a normal non virtual scroll container (right scroll), you can note that the data remains the same and does not change.
See also the animated gif below.
I don't know what might be causing the problem, but it seems that it's caused by the virtual scroll functionality.
I've encounted this problem in v7 but it is also present in v8.
Also note that the values that are passed as an @input() are not changed but remain the same.
Expected Behavior
It should be expected that the data that was retrieved onInit is maintained and rendered correctly.
Environment
The text was updated successfully, but these errors were encountered: