Virtual Scroll template caching breaking behavior for Components with ngOnInit
- Langage dominant
- TypeScript
- Étoiles
- 25k
- Forks
- 6.8k
- Merge moyen
- 1 j 8 h
- PR mergées (30 j)
- 91
Description
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 (see ```app.component.html```). The ```PersonComponent``` takes a ```Person``` object as an input, and onInit retrieves an ```Activity``` 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 the ```PersonComponent``` 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,
- Open the stack blits above
- Take note of the Names and their associated activity.
- Scroll down on the virtual scroll container (left scroll), and then all the way up
- Note that the activities associated with the names have changed.
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
- Angular: v8.0.1
- CDK/Material: v8.0.1 (also present in v7.3.6)
- Browser: Chrome
- Operating System: Ubuntu
Guide de contribution
Ouvrir le guide de contribution
Piste de recherche
Commencez par le StackBlitz lié, en particulier app.component.html, PersonComponent et ActivityService, et reproduisez le problème en faisant défiler vers le bas puis vers le haut avec la mise en cache des templates activée. Comparez le comportement de virtual-scroll et de normal-scroll, puis suivez le cycle de vie de la vue CDK virtual-scroll. Le travail est terminé lorsque l’activité reste associée à la bonne personne après plusieurs défilements et que la reproduction existante n’affiche plus de valeurs incorrectes.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- angular, typescript
- Domaine
- frontend
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 35/100