-
I've been looking into the internal implementation of ng-zorro-antd and found several occasions where <ng-template #formatTemplate>
<ng-container *nzStringTemplateOutlet="formatter; context: { $implicit: nzPercent }; let formatter">
{{ formatter(nzPercent) }}
</ng-container>
</ng-template> (see original code) I believe this complicated code is equivalent to a much simpler version <ng-template #formatTemplate>
{{ formatter(nzPercent) }}
</ng-template> What is the reason of using Any hints or comments are greatly appreciated. Details on the equivalency of the two code snippets aboveThe complicated version will be transformed by Angular into something like this <ng-template #formatTemplate>
<ng-container
[nzStringTemplateOutlet]="formatter"
[nzStringTemplateOutletContext]="{ $implicit: nzPercent }"
let-formatter="$implicit">
{{ formatter(nzPercent) }}
</ng-container>
</ng-template> Here private recreateView(): void {
this.viewContainer.clear();
const isTemplateRef = this.nzStringTemplateOutlet instanceof TemplateRef;
const templateRef = (isTemplateRef ? this.nzStringTemplateOutlet : this.templateRef) as NzSafeAny;
this.embeddedViewRef = this.viewContainer.createEmbeddedView(
templateRef,
isTemplateRef ? this.nzStringTemplateOutletContext : this.context
);
}
private updateContext(): void {
const isTemplateRef = this.nzStringTemplateOutlet instanceof TemplateRef;
const newCtx = isTemplateRef ? this.nzStringTemplateOutletContext : this.context;
const oldCtx = this.embeddedViewRef!.context as NzSafeAny;
if (newCtx) {
for (const propName of Object.keys(newCtx)) {
oldCtx[propName] = newCtx[propName];
}
}
} (again, the original code is here)
if (nzStringTemplateOutlet) {
this.context.$implicit = nzStringTemplateOutlet.currentValue;
} So the part
basically defines a template input variable named 'formatter' which references the function NzProgressComponent_ng_template_0_span_0_ng_template_2_ng_container_0_Template(rf, ctx) { if (rf & 1) {
_angular_core__WEBPACK_IMPORTED_MODULE_2__["ɵɵelementContainerStart"](0);
_angular_core__WEBPACK_IMPORTED_MODULE_2__["ɵɵtext"](1);
_angular_core__WEBPACK_IMPORTED_MODULE_2__["ɵɵelementContainerEnd"]();
} if (rf & 2) {
const formatter_r9 = ctx.$implicit;
const ctx_r8 = _angular_core__WEBPACK_IMPORTED_MODULE_2__["ɵɵnextContext"](4);
_angular_core__WEBPACK_IMPORTED_MODULE_2__["ɵɵadvance"](1);
_angular_core__WEBPACK_IMPORTED_MODULE_2__["ɵɵtextInterpolate1"](" ", formatter_r9(ctx_r8.nzPercent), " ");
} } Note that To summarize, the
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Lots of properties including <ng-container *nzStringTemplateOutlet="formatter; context: { $implicit: nzPercent }; let formatter">
{{ formatter(nzPercent) }}
</ng-container> In this example, if In conclusion, we could not simplify this. |
Beta Was this translation helpful? Give feedback.
Lots of properties including
nzPercent
acceptTemplateRef
orstring
.nzStringTemplateOutlet
is for dealing with this situation.In this example, if
nzPercent
is a template, we will render it and inject the context. Otherwise, we would directly render{{ formatter(nzPercent) }}
.In conclusion, we could not simplify this.