refactor(common): drop ngIf assert template error in production (#59705)

This commit removes the `assertTemplate` error message in production.

PR Close #59705
This commit is contained in:
arturovt
2025-01-24 17:25:32 +02:00
committed by Andrew Kushnir
parent 92250493ff
commit 0f3fa5a2af
3 changed files with 20 additions and 6 deletions
+15 -6
View File
@@ -13,8 +13,11 @@ import {
TemplateRef,
ViewContainerRef,
ɵstringify as stringify,
ɵRuntimeError as RuntimeError,
} from '@angular/core';
import {RuntimeErrorCode} from '../errors';
/**
* A structural directive that conditionally includes a template based on the value of
* an expression coerced to Boolean.
@@ -185,7 +188,7 @@ export class NgIf<T = unknown> {
*/
@Input()
set ngIfThen(templateRef: TemplateRef<NgIfContext<T>> | null) {
assertTemplate('ngIfThen', templateRef);
assertTemplate(templateRef, (typeof ngDevMode === 'undefined' || ngDevMode) && 'ngIfThen');
this._thenTemplateRef = templateRef;
this._thenViewRef = null; // clear previous view if any.
this._updateView();
@@ -196,7 +199,7 @@ export class NgIf<T = unknown> {
*/
@Input()
set ngIfElse(templateRef: TemplateRef<NgIfContext<T>> | null) {
assertTemplate('ngIfElse', templateRef);
assertTemplate(templateRef, (typeof ngDevMode === 'undefined' || ngDevMode) && 'ngIfElse');
this._elseTemplateRef = templateRef;
this._elseViewRef = null; // clear previous view if any.
this._updateView();
@@ -263,9 +266,15 @@ export class NgIfContext<T = unknown> {
public ngIf: T = null!;
}
function assertTemplate(property: string, templateRef: TemplateRef<any> | null): void {
const isTemplateRefOrNull = !!(!templateRef || templateRef.createEmbeddedView);
if (!isTemplateRefOrNull) {
throw new Error(`${property} must be a TemplateRef, but received '${stringify(templateRef)}'.`);
function assertTemplate(
templateRef: TemplateRef<any> | null,
property: string | false | null,
): void {
if (templateRef && !templateRef.createEmbeddedView) {
throw new RuntimeError(
RuntimeErrorCode.NG_IF_NOT_A_TEMPLATE_REF,
(typeof ngDevMode === 'undefined' || ngDevMode) &&
`${property} must be a TemplateRef, but received '${stringify(templateRef)}'.`,
);
}
}
+3
View File
@@ -15,6 +15,9 @@ export const enum RuntimeErrorCode {
PARENT_NG_SWITCH_NOT_FOUND = 2000,
EQUALITY_NG_SWITCH_DIFFERENCE = 2001,
// NgIf errors
NG_IF_NOT_A_TEMPLATE_REF = 2020,
// Pipe errors
INVALID_PIPE_ARGUMENT = 2100,