question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(cdk-virtual-scroll-viewport): emit events when cached templates are added/removed from the DOM

See original GitHub issue

Reproduction

Use StackBlitz to reproduce your issue:

Steps to reproduce:

  1. Delete element by delete button
  2. 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
de-doncommented, Apr 29, 2021

I have the same issue.

I think it would be cool to be able to delete the cache for a specific element by key from trackBy:

public removeElement(item) {
  // HERE IS REMOVING LOGIC
  // ...
  this.cdkVirtualScrollViewport.clearItemCache(item.id);
}
0reactions
angular-automatic-lock-bot[bot]commented, Apr 13, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found