angular-docs-cn/packages/compiler-cli/test/compliance/test_cases
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
..
r3_compiler_compliance refactor(compiler-cli): implement `ɵɵngDeclarePipe()` (#40803) 2021-02-12 09:00:16 -08:00
r3_view_compiler refactor(compiler-cli): include `template` source directly inside declaration object (#40383) 2021-01-11 15:37:12 -08:00
r3_view_compiler_bindings refactor(compiler-cli): implement `ɵɵngDeclarePipe()` (#40803) 2021-02-12 09:00:16 -08:00
r3_view_compiler_di/di refactor(compiler-cli): implement `ɵɵngDeclarePipe()` (#40803) 2021-02-12 09:00:16 -08:00
r3_view_compiler_directives/matching refactor(compiler-cli): include `template` source directly inside declaration object (#40383) 2021-01-11 15:37:12 -08:00
r3_view_compiler_i18n refactor(compiler-cli): implement `ɵɵngDeclarePipe()` (#40803) 2021-02-12 09:00:16 -08:00
r3_view_compiler_input_outputs refactor(compiler-cli): include `template` source directly inside declaration object (#40383) 2021-01-11 15:37:12 -08:00
r3_view_compiler_listener refactor(compiler-cli): include `template` source directly inside declaration object (#40383) 2021-01-11 15:37:12 -08:00
r3_view_compiler_providers refactor(compiler-cli): include `template` source directly inside declaration object (#40383) 2021-01-11 15:37:12 -08:00
r3_view_compiler_styling refactor(compiler-cli): implement `ɵɵngDeclarePipe()` (#40803) 2021-02-12 09:00:16 -08:00
r3_view_compiler_template refactor(compiler): retrieve variables from context inside nested template listener (#40833) 2021-02-17 11:45:46 -08:00
source_mapping refactor(compiler-cli): implement `ɵɵngDeclarePipe()` (#40803) 2021-02-12 09:00:16 -08:00
BUILD.bazel test(compiler-cli): generate golden files for partial compilation (#39617) 2020-11-13 11:25:56 -08:00
list_golden_update_rules.ts refactor(compiler-cli): use semver range checking for partial versions (#39847) 2020-12-04 10:26:17 -08:00
test_case_schema.json test(compiler-cli): improve compliance test compile mode filtering (#39939) 2020-12-07 16:21:04 -08:00