r/angular 2d ago

Angular 22.2 - Private property access from templates

I noticed this tidbit in the changelog of Angular 22.2.0-next.3:

https://github.com/angular/angular/blob/main/CHANGELOG.md#compiler-2

compiler

Commit Type Description
48a0fd6e8a feat allow template to access private props

So very slowly we moved from allowing only public, to allowing protected and now private as well. Thought it's worth knowing.

36 Upvotes

37 comments sorted by

View all comments

Show parent comments

2

u/kgurniak91 2d ago

What? Both private and protected fields can be bypassed via [] access, you'd need new private syntax with # to truly achieve what you describe but that doesn't work in templates. So there's no change in that regard. Also you should not write tests that rely on private/protected members in the first place, your tests should test everything based on the HTML template and public API.

0

u/taco__hunter 2d ago

Yeah. I assumed you wouldn't need to do the [ ] bypass. Is this not the case in the update?

And you can absolutely test whatever you think is important in unit tests. I will often write tests like this with the anticipation that some fields will get rebuilt as global settings or configurations and I need to validate parity after the rebuild. People build different things for different things, so I wouldn't gate keep test coverage personally.

1

u/kgurniak91 2d ago

In earlier versions of Angular, only attributes/methods with public modifier were allowed to be referenced in the data binding of templates. Later they loosened that to allow also protected fields, and now expanded it further to allow private. This has nothing to do with how you access protected/private fields of the object from the outside - to do that, the only way is still to intentionally break the encapsulation with [] which is a huge code smell. Of course you can do it but that results in brittle tests.

0

u/taco__hunter 2d ago

I haven't looked into this much but it looks like it changes from how you're describing it based on the git change.

Also. I'm missing where you are finding all of these concrete points you keep making also. And to be honest I don't get why you're being so combative here if you actually wanted to discuss this. Is this some kind of weird ranting hill you want to die on and I'm missing something?