Skip to content

Commit 44d0d88

Browse files
authored
fix(list-view): always set a default template (#16)
Also logs when we try to create a view without a valid template key
1 parent dd0d3b1 commit 44d0d88

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ export class ListViewComponent<T = any> implements DoCheck, OnDestroy, AfterCont
147147
// The itemTemplateQuery may be changed after list items are added that contain <template> inside,
148148
// so cache and use only the original template to avoid errors.
149149
this.fallbackItemTemplate = this.itemTemplateQuery;
150-
if (this._templateMap) {
151-
// sometimes templates are registered before loader is ready, so we update here
152-
this._templateMap.forEach((t) => (t.location = this.loader));
153-
} else if (this.fallbackItemTemplate) {
150+
if (this.fallbackItemTemplate && !this._templateMap?.has('default')) {
154151
// apparently you can create a Core ListView without a template...
152+
// we also add a fallback default for when the user sets multiple templates but no templateSelector
155153
this.registerTemplate('default', this.fallbackItemTemplate);
156154
}
157155

158156
if (this._templateMap) {
157+
// sometimes templates are registered before loader is ready, so we update here
158+
this._templateMap.forEach((t) => (t.location = this.loader));
159159
if (NativeScriptDebug.isLogEnabled()) {
160160
NativeScriptDebug.listViewLog('Setting templates');
161161
}
@@ -219,6 +219,12 @@ export class ListViewComponent<T = any> implements DoCheck, OnDestroy, AfterCont
219219
// this should never enter if it creates the view
220220
const templateKey = typeof lview.itemTemplateSelector === 'function' ? lview.itemTemplateSelector(currentItem, index, items) : 'default';
221221
template = this._templateMap.get(templateKey);
222+
if (!template) {
223+
if (NativeScriptDebug.isLogEnabled()) {
224+
NativeScriptDebug.listViewError(`Template for key '${templateKey}' not found.`);
225+
}
226+
return;
227+
}
222228
args.view = template.create({ index, data: currentItem });
223229
}
224230
this.setupViewRef(template.getEmbeddedViewRef(args.view), currentItem, index, args.view);

0 commit comments

Comments
 (0)