cdf1ea1951
This is a pre-requisite for #40360. Given the following template which has a listener that references a variable from a parent template (`name`): ``` <ng-template let-name="name"> <button (click)="hello(name)"></button> </ng-template> ``` We generate code that looks that looks like. Note how we access `name` through `ctx`: ```js function template(rf, ctx) { if (rf & 1) { const r0 = ɵɵgetCurrentView(); ɵɵelementStart(0, "button", 2); ɵɵlistener("click", function() { ɵɵrestoreView(r0); const name_r0 = ctx.name; // Note the `ctx.name` access here. const ctx_r1 = ɵɵnextContext(); return ctx_r1.log(name_r0); }); ɵɵelementEnd(); } } ``` This works fine at the moment, because the template context object can't be changed after creation. The changes in #40360 allow for the object to be changed, which means that the `ctx` reference inside the listener will be out of date, because it was bound during creation mode. This PR aims to address the issue by accessing the context inside listeners through the saved view reference. With the new code, the generated code from above will look as follows: ```js function template(rf, ctx) { if (rf & 1) { const r0 = ɵɵgetCurrentView(); ɵɵelementStart(0, "button", 2); ɵɵlistener("click", function() { const restoredCtx = ɵɵrestoreView(r0); const name_r0 = restoredCtx.name; const ctx_r1 = ɵɵnextContext(); return ctx_r1.log(name_r0); }); ɵɵelementEnd(); } } ``` PR Close #40833 |
||
---|---|---|
.. | ||
mock_compile | ||
prelink | ||
BUILD.bazel | ||
README.md | ||
mock_compiler_spec.ts | ||
r3_compiler_compliance_spec.ts | ||
r3_view_compiler_binding_spec.ts | ||
r3_view_compiler_di_spec.ts | ||
r3_view_compiler_directives_spec.ts | ||
r3_view_compiler_i18n_spec.ts | ||
r3_view_compiler_input_outputs_spec.ts | ||
r3_view_compiler_listener_spec.ts | ||
r3_view_compiler_providers_spec.ts | ||
r3_view_compiler_spec.ts | ||
r3_view_compiler_styling_spec.ts | ||
r3_view_compiler_template_spec.ts |
README.md
Tests in this directory should be run with:
yarn bazel test --config=ivy packages/compiler-cli/test/compliance:compliance