feat(cdk-virtual-scroll-viewport): emit events when cached templates are added/removed from the DOM
See original GitHub issueReproduction
Use StackBlitz to reproduce your issue:
Steps to reproduce:
- Delete element by delete button
- Show console -> do not see the deletion message
I have directive which shows tooltips
@Directive({ selector: "[tooltip]" })
export class TooltipDirective implements OnDestroy {
public id =
"_" +
Math.random()
.toString(36)
.substr(2, 9);
public ngOnInit() {
console.log("ngOnInit", this.id);
}
public ngOnDestroy() {
// not working when removed element (also remove from model)
console.log("ngOnDestroy", this.id);
}
}
but when I delete element, the cache is no longer needed and is not relevant, because I deleted the element from the model, which means the ngOnDestroy
method should work
<cdk-virtual-scroll-viewport
minBufferPx="200"
maxBufferPx="200"
[itemSize]="24"
[style.height.px]="200"
>
<ng-container *cdkVirtualFor="let favorite of favorites">
<ng-template
[ngTemplateOutlet]="virtualRef"
[ngTemplateOutletContext]="{ $implicit: favorite }"
>
</ng-template>
</ng-container>
</cdk-virtual-scroll-viewport>
<ng-template #virtualRef let-favorite>
<div class="favorite-line" tooltip="hello world">
{{ favorite.title }}
<button (click)="remove(favorite)">delete</button>
</div>
</ng-template>
public remove(favorite: Favorite): void {
this.favorites = this.favorites.filter(
(item: Favorite): boolean => item.id !== favorite.id
);
}
Expected Behavior
What behavior were you expecting to see?
When deleting a model, a template with an inactive model should not be cached, since this model no longer exists
Actual Behavior
What behavior did you actually see?
Workaround to use templateCacheSize: 0
, but this is not a solution to the problem, because we are working with heavy templates that need to be cached.
The ngOnDestroy
method is not called, although we removed an element from the array
Environment
- Angular: latest
- CDK/Material: latest
- Browser(s): any
- Operating System (e.g. Windows, macOS, Ubuntu): any
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Scrolling | Angular Material
The scrolling package provides helpers for directives that react to scroll events. link cdkScrollable and ScrollDispatcher. The cdkScrollable directive and ...
Read more >Make sort work with CdkVirtualFor and templateCacheSize
But when I sort my data and scroll, the cached templates screws up the items in the view. In other words: I sort...
Read more >Pre-Compiling And Caching HTML Templates - · Los Techies
Only the data that is rendered in to the template changes. Simple Template Caching. The easiest way of storing a template is to...
Read more >Angular Performance: Optimize Template Expressions
DOM events e.g. click, changes, mouseover, etc. setTimeout, setInterval, etc. Implicit CD call by the component. Performance Bottleneck on ...
Read more >Formie - Cached Forms - Verbb
If you are using the {% cache %} Twig tag in your templates, ... addEventListener('DOMContentLoaded', (event) => { // Fetch the form we...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have the same issue.
I think it would be cool to be able to delete the cache for a specific element by
key
fromtrackBy
:This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.