Skip to content

Using viewChild/viewChildren with private property should be an error #54863

Closed

Description

@csvn

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

core

Is this a regression?

No

Description

Using a private property (e.g. #children) with viewChildren() or viewChild() will silently fail, and never update the counts. This is likely also the same for contentChild() and contentChildren(). This also means that retrieving the signal in an effect() will also never trigger outside of the initial run.

Current workaround is to use private myProp = viewChildren('selector');, which will work as intended while still hiding the property from being public.

Please provide a link to a minimal reproduction of the bug

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

Please provide the exception or error you saw

A `viewChildren()` on a public property (`elements`) will log 1, while a `viewChildren()` on private property (`#elements`) will log 0.

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

Angular: 17.3.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, 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.3.3
zone.js                         0.14.4

Anything else?

No response

Activity

  1. changed the title [-]Using viewChild/viewChildren with private property breaks[/-] [+]Using viewChild/viewChildren with private property should be an error[/+] on Mar 14, 2024
  2. moved this to Not Started in Reactivityon Mar 14, 2024
  3. alxhub commented on Mar 14, 2024

    @alxhub
    Member

    This shouldn't be allowed by the compiler in the first place. I believe we want to enforce that such properties are at least protected.

  4. added
    core: reactivity
    on Mar 14, 2024
  5. csvn commented on Mar 14, 2024

    @csvn
    ContributorAuthor

    That's perfectly fine to produce an error, if unsupported. It was just a headache to find out the reason why the code was not working 😅

  6. self-assigned this
    on Mar 14, 2024
  7. moved this from Not Started to Done in Reactivityon Mar 27, 2024
  8. added a commit that references this issue on Mar 27, 2024
    6219341
  9. added a commit that references this issue on Mar 27, 2024
    c6b68b0
  10. added a commit that references this issue on Mar 28, 2024
    b478dfb
  11. angular-automatic-lock-bot commented on Apr 27, 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.

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

Metadata

Metadata

Assignees

Labels

bugcore: reactivitycross-cutting: signals

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions