Skip to content

Commit 913ce2f

Browse files
committed
feat: provide TEMPLATED_ITEMS_COMPONENT
1 parent 7496735 commit 913ce2f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/angular/src/lib/cdk/list-view/list-view.component.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterContentInit, ChangeDetectionStrategy, Component, ContentChild, Directive, DoCheck, ElementRef, EmbeddedViewRef, EventEmitter, Host, HostListener, Input, IterableDiffer, IterableDiffers, NgZone, OnDestroy, Output, TemplateRef, ViewChild, ViewContainerRef, ɵisListLikeIterable as isListLikeIterable, ɵmarkDirty } from '@angular/core';
1+
import { AfterContentInit, ChangeDetectionStrategy, Component, ContentChild, Directive, DoCheck, ElementRef, EmbeddedViewRef, EventEmitter, forwardRef, Host, HostListener, Inject, InjectionToken, Input, IterableDiffer, IterableDiffers, NgZone, OnDestroy, Output, TemplateRef, ViewChild, ViewContainerRef, ɵisListLikeIterable as isListLikeIterable, ɵmarkDirty } from '@angular/core';
22
import { ItemEventData, KeyedTemplate, LayoutBase, ListView, ObservableArray, profile, View } from '@nativescript/core';
33

44
import { extractSingleViewRecursive } from '../../element-registry/registry';
@@ -7,6 +7,12 @@ import { NgViewTemplate } from '../../view-refs';
77

88
const NG_VIEW = '_ngViewRef';
99

10+
export interface TemplatedItemsHost<T = any> {
11+
registerTemplate(key: string, template: TemplateRef<T>);
12+
}
13+
14+
export const TEMPLATED_ITEMS_COMPONENT = new InjectionToken<TemplatedItemsHost>('TemplatedItemsComponent');
15+
1016
export class ItemContext<T> {
1117
constructor(public $implicit?: T, public item?: T, public index?: number, public even?: boolean, public odd?: boolean) {}
1218
}
@@ -83,8 +89,9 @@ export interface SetupItemViewArgs<T> {
8389
<ng-container #loader></ng-container>
8490
</DetachedContainer>`,
8591
changeDetection: ChangeDetectionStrategy.OnPush,
92+
providers: [{ provide: TEMPLATED_ITEMS_COMPONENT, useExisting: forwardRef(() => ListViewComponent) }],
8693
})
87-
export class ListViewComponent<T = any> implements DoCheck, OnDestroy, AfterContentInit {
94+
export class ListViewComponent<T = any> implements DoCheck, OnDestroy, AfterContentInit, TemplatedItemsHost {
8895
public get nativeElement(): ListView {
8996
return this.templatedItemsView;
9097
}
@@ -180,7 +187,10 @@ export class ListViewComponent<T = any> implements DoCheck, OnDestroy, AfterCont
180187
this._templateMap = new Map<string, NsTemplatedItem<T>>();
181188
}
182189

183-
this._templateMap.set(key, new NsTemplatedItem<T>(template, this.loader, (v) => this._viewToTemplate.set(v, key)));
190+
this._templateMap.set(
191+
key,
192+
new NsTemplatedItem<T>(template, this.loader, (v) => this._viewToTemplate.set(v, key))
193+
);
184194
}
185195

186196
@HostListener('itemLoading', ['$event'])
@@ -263,7 +273,7 @@ export function getItemViewRoot(viewRef: EmbeddedViewRef<unknown>, rootLocator:
263273
// eslint-disable-next-line @angular-eslint/directive-selector
264274
@Directive({ selector: '[nsTemplateKey],[nsTemplateKeys]' })
265275
export class TemplateKeyDirective<T> {
266-
constructor(private templateRef: TemplateRef<T>, @Host() private comp: ListViewComponent<T>) {}
276+
constructor(private templateRef: TemplateRef<T>, @Host() @Inject(TEMPLATED_ITEMS_COMPONENT) private comp: TemplatedItemsHost<T>) {}
267277

268278
@Input()
269279
set nsTemplateKey(value: string) {

0 commit comments

Comments
 (0)