Skip to content

Using this keyword in templates will use template varibales instead of component variables. #55115

Closed

Description

@wartab

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

No

Description

When defining a variable inside of a template (for example setting a variable with $index in a loop), using this. in combination with that variable will resolve to the local template variable and not the component variable.

@Component({
    selector: "app",
    standalone: true,
    template: `
        @for (item of list; track item; let idx = $index) {
            {{ this.idx }} {{ item }}
        }
    `,
})
export class App {
    public list = ["hello"];
    public idx = "this should be idx";
}

This shows 0 hello instead of this should be idx hello.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-iq5cdb?file=src%2Fmain.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 17.3.0
Node: 20.10.0
Package Manager: npm 9.6.4
OS: win32 x64

Angular: 17.3.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, language-service, localize, material
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1703.0
@angular-devkit/build-angular   17.3.0
@angular-devkit/core            17.3.0
@angular-devkit/schematics      17.3.0
@schematics/angular             17.3.0
rxjs                            7.8.1
typescript                      5.4.2
zone.js                         0.14.4

Anything else?

We noticed this by accident, obviously not a show-stopper, we just have a co-worker who's convinced he should use the this keyword in templates and doesn't want to stop.

Activity

  1. added
    area: core
    core: control flow
    on Mar 29, 2024
  2. added this to the needsTriage milestone on Mar 29, 2024
  3. added
    area: compiler
    and removed
    area: core
    core: control flow
    on Mar 29, 2024
  4. assigned and unassigned on Mar 29, 2024
  5. dylhunn commented on Apr 2, 2024

    @dylhunn
    Contributor

    IIRC this is not a regression -- in fact, we deliberately reimplemented this bug in Template Pipeline to avoid breaking applications. Now that TemplateDefinitionBuilder is deleted, it might be time to try fixing this for good, and get a sense of how breaking that change would be.

  6. 2 remaining items

  7. added a commit that references this issue on Oct 7, 2024
    0c8817e
  8. added a commit that references this issue on Oct 8, 2024
    09f589f
  9. angular-automatic-lock-bot commented on Nov 8, 2024

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  10. locked and limited conversation to collaborators on Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilerbugcompiler: template pipeline

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions