angular/packages/compiler/src
Kristiyan Kostadinov cdf1ea1951 refactor(compiler): retrieve variables from context inside nested template listener (#40833)
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
2021-02-17 11:45:46 -08:00
..
aot fix(compiler): update type castings for JSON.parse usage (#40710) 2021-02-09 10:48:43 -08:00
compiler_util refactor(compiler): store the fullStart location on ParseSourceSpans (#39486) 2020-11-06 09:01:37 -08:00
expression_parser fix(compiler): include parenthesis in expression source spans (#40740) 2021-02-10 11:07:11 -08:00
i18n docs: update links to use HTTPS as protocol (#39718) 2020-11-20 12:52:16 -08:00
jit docs: update links to use HTTPS as protocol (#39718) 2020-11-20 12:52:16 -08:00
ml_parser fix(compiler): exclude trailing whitespace from element source spans (#40513) 2021-01-28 08:53:02 -08:00
output fix(core): address Trusted Types bug in Chrome 83 (#40815) 2021-02-12 16:59:58 -08:00
render3 refactor(compiler): retrieve variables from context inside nested template listener (#40833) 2021-02-17 11:45:46 -08:00
schema refactor(compiler-cli): introduce APIs to support directive autocompletion (#40032) 2020-12-14 12:08:41 -08:00
template_parser fix(compiler): exclude trailing whitespace from element source spans (#40513) 2021-01-28 08:53:02 -08:00
view_compiler fix(core): QueryList should not fire changes if the underlying list did not change. (#40091) 2021-01-14 13:55:02 -08:00
assertions.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
ast_path.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
chars.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
compile_metadata.ts fix(core): QueryList should not fire changes if the underlying list did not change. (#40091) 2021-01-14 13:55:02 -08:00
compile_reflector.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
compiler.ts refactor(compiler-cli): implement ɵɵngDeclarePipe() (#40803) 2021-02-12 09:00:16 -08:00
compiler_facade_interface.ts refactor(compiler-cli): implement ɵɵngDeclarePipe() (#40803) 2021-02-12 09:00:16 -08:00
config.ts refactor(compiler): remove extra imports (#37246) 2020-06-11 19:00:33 -07:00
constant_pool.ts feat(compiler): support tagged template literals in code generator (#39122) 2020-12-07 16:20:04 -08:00
core.ts refactor(core): Remove the need for explicit static query instruction (#40091) 2021-01-14 13:55:02 -08:00
directive_normalizer.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
directive_resolver.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
identifiers.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
injectable_compiler.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
injectable_compiler_2.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
jit_compiler_facade.ts refactor(compiler-cli): implement ɵɵngDeclarePipe() (#40803) 2021-02-12 09:00:16 -08:00
lifecycle_reflector.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
metadata_resolver.ts fix(core): QueryList should not fire changes if the underlying list did not change. (#40091) 2021-01-14 13:55:02 -08:00
ng_module_compiler.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
ng_module_resolver.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
parse_util.ts refactor(compiler): store the fullStart location on ParseSourceSpans (#39486) 2020-11-06 09:01:37 -08:00
pipe_resolver.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
provider_analyzer.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
resource_loader.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
selector.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
shadow_css.ts fix(compiler): support multiple selectors in :host-context() (#40494) 2021-02-16 08:41:19 -08:00
style_compiler.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
style_url_resolver.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
summary_resolver.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
url_resolver.ts docs: remove unused "docregions" (#40479) 2021-01-20 16:12:15 -08:00
util.ts perf(compiler): use raw bytes to represent utf-8 encoded strings (#39694) 2020-11-17 10:09:28 -08:00
version.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00