I encountered a new error in my Angular application that I haven't seen before. The issue is arising from this specific line in the Angular source code.
This error occurs when I log out and then log back into my app. While on a certain route, there is an observable being observed. When this observable emits a value to its stream, it triggers updates and change detection, resulting in elements being removed and added dynamically.
I suspect the component below is causing the error. Can anyone identify any obvious mistakes I'm making? I have included what I believe could be relevant to the bug and commented out everything else. It's possible that I am misusing ngTemplateOutlet
and ContentChild
here...
Component:
@Component({
selector: 'paginated-infinite-scroll',
templateUrl: './paginated-infinite-scroll.component.html'
})
export class PaginatedInfiniteScrollComponent {
// ...
@ContentChild('results') resultsRef: TemplateRef<any>;
@ContentChild('noResults') noResultsRef: TemplateRef<any>;
@ContentChild('loading') loadingRef: TemplateRef<any>;
@ContentChild('loadingMore') loadingMoreRef: TemplateRef<any>;
// ...
}
Template:
<div infiniteScroll [infiniteScrollDistance]="2" [infiniteScrollThrottle]="300" [scrollWindow]="true" (scrolled)="scrolled()">
<ng-container *ngIf="result.items.length > 0 && !loading">
<ng-template [ngTemplateOutlet]="resultsRef"></ng-template>
</ng-container>
<ng-container *ngIf="result.items.length === 0 && !loading">
<ng-template [ngTemplateOutlet]="noResultsRef"></ng-template>
</ng-container>
<ng-container *ngIf="loading">
<ng-template [ngTemplateOutlet]="loadingRef"></ng-template>
</ng-container>
<ng-container *ngIf="loadingMore">
<ng-template [ngTemplateOutlet]="loadingMoreRef"></ng-template>
</ng-container>
</div>
Usage:
<paginated-infinite-scroll [onScroll]="onScroll" [query]="query" [result]="result">
<ng-template #results>
// Display results looped through here...
</ng-template>
<ng-template #noResults>
// Show message for no results here...
</ng-template>
<ng-template #loading>
// Display loading spinner here...
</ng-template>
<ng-template #loadingMore>
// Show alternative loading spinner here...
</ng-template>
</paginated-infinite-scroll>