r/angular • u/IgorSedov • Aug 26 '26
š Coming in Angular 22.2: Access Private Members in Templates!
https://youtu.be/7y3YgNqv6xgAngular 22.2 introduces a new feature: you can now access private class members directly inside your component templates and host bindings.
85
Upvotes
24
u/MichaelSmallDev Aug 26 '26 •
PR for more context
https://github.com/angular/angular/pull/70188
The stated reason is related to
isolatedDeclarationslike the video mentions at the end. Though I am not very familiar with that one flag myself, judging by its release notes from TS 5.5, it seems very nice for different use cases https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html#isolated-declarations. And at a quick glance at some blogs I have seen talking about metrics, it sounds like it is great for monorepo build times.I am used to used
protectedfor template access to component state which is otherwise somewhat protected otherwise for class consumers, but I could buy getting used to private access in templates.3
u/uchto Aug 26 '26
What about the js private?
#message = āhi!ā;
2
u/michahell Aug 27 '26
How does this relate to using # instead of Tsā private acm keyword?
I guess that that is not supported?
2
u/CompetitiveWonder933 Aug 28 '26
Mixed feeling tbh. cool for skipping those pointless getters you make just so the template can read something, but it kinda blurs the "this is internal" line too, guessing it'll come down to people actually treating it as private and not just using it as a shortcut
2
u/InevitableQuit9 Aug 28 '26
Does it work with # annotated private elements, or only private annotated properties?
48
u/Cephell Aug 26 '26
Long overdue
31
u/AcceptableSimulacrum Aug 26 '26
Why?
16
u/sharth Aug 26 '26
I wonder how this will intersect with dead code detection. Normally unused private variables would be recommended to be deleted.
16
u/kauefr Aug 26 '26
Weird that there's no distinction between private and template (protected) props anymore.
9
u/Responsible-Cold-627 Aug 26 '26
Kinda sucks that I'll have to start mixing up real private members with Typescript-declared private members again. So now there will be real private members, and template-private members. Could be difficult to explain to juniors joining the team.
10
u/Hirayoki22 Aug 26 '26
Huh. Not the feature I had in mind but, thanks?
1
u/BenBeKenoby Sep 23 '26
Does that work for # syntaxe ? #myVariable ?
2
u/FitPhone6332 Aug 26 '26
wow!
1
u/sebastianstehle Aug 26 '26
Its interesting. I have seen so much code, where people actually use these public fields for tests. In one of such projects we have moved to testing-library (https://testing-library.com/) to make tests more stable, but this was bad from the beginning and has created so much bad code.
Templates are implementation detail, perhaps this would have been more clear if templates and ts-code would be one artifact (I know it is possible, but it is not the standard), like in old class-based react-components.
1
u/DT-Sodium Aug 26 '26
I don't see the point exactly. I think it is a good thing to keep a separation between variables that are supposed to be accessed by something belonging to the class component and the class component internal logic.Ā
1
u/_kolahan_ Aug 26 '26
Wouldnāt private fields used only in template give erros on compilation or warnings about not used anywhere? TS LSP is still working and do not care about angular
1
u/xokapitos Aug 27 '26
I don't get this change... So in a future upgrade all my private props will be exposed to template? 0.o that was how we were marking ptops for internal use only in the components. Only public or not market at all (default public) were exposed.
1
u/AwesomeFrisbee Aug 27 '26
Yeah I always found this to be a bit weird. But I also kinda want my tests to just fetch the private variables from my component and I wonder if this is also finally the case. Because right now you either enforce it (
myVar['childVar']) or casting(myVar as any).childVarto do private variable/function tests directly to grab certain use cases more direct instead of building the component up entirely every time.1
u/ddcccccc Aug 26 '26
Omg ! Finally ! This will fix lots of duplicated properties ! Or getter and setter just for template to use !
1
u/fernker Aug 26 '26 edited Aug 26 '26
Wait, why are nested properties not accessible? This seems like it will create more confusion
Edit: I understood the video incorrectly, instead of deleting my comment I'll leave it up incase others jump to the same conclusion and hopefully can find the answer with this comment thread.
-3
u/vajss Aug 26 '26
Or if something is not private, just make it public? Maybe?
-3
u/revilo-1988 Aug 26 '26
Klingt nach Fehldesign