Accessing component's private members from the template

1,632 views
Skip to first unread message

Yaroslav Admin

unread,
Aug 16, 2016, 9:47:37 AM8/16/16
to AngularJS
Hello,

I'm looking for an official position on accessing component's private variables/methods from template. I am using TypeScript and it works, but is it conceptually correct way of doing things?

The only thing I found on the matter is this question on SO: http://stackoverflow.com/q/34574167/1377864. I completely agree with what drewmoore said, but Günter Zöchbauer also made good point about Dart and Dart is officially supported by Angular 2... So now I'm not sure.

My original motivation is to open issue for WebStorm, that component's private variables/methods used only inside templates are reported as unused variables, but I would probably need some proof, that accessing private variables inside template is an intended behaviour and not a coincidence. Any thoughts?

Best regards,
Yaroslav.

Sander Elias

unread,
Aug 17, 2016, 8:20:06 AM8/17/16
to AngularJS
Hi Yarslav,

In typescript a private is intended to be used within the class itself, and any access from the outside should be prohibited. Technically, privates should be inaccessible from the template. Due to the fact that it compiles down to JS, and TS only does type-checking during dev and compile time, it is possible to access privates from the template. However, with ES6 it is possible to create true private instance variables. A future version of TS might utilize this. When/If it does this, this option will render templates that relay on private instance vars useless.

While I like the sentiment that DrewMoore is expressing, strictly speaking, using privates inside templates is not ok. They work because there is a void between TS and the level of protection ES5 can deliver. 

That's a TS issue more as it has anything to do with NG. 

advice, don't use TS privates at all, as this is a confusing issue. If you insist on privates, don't use them in the templates, but be aware that they are in fact available (no so private at all!)
if you really wan't privates, use some symbols, there are a couple of good blogs around how to create real privates in ES6, and even in ES5 (although that's much harder to do, and not 100%)

Regards
Sander



Yaroslav Admin

unread,
Aug 17, 2016, 10:04:25 AM8/17/16
to AngularJS
Hi Sanders,

Thanks for your reply. Your answer pointed me in the correct direction. Now I'm convinced, that I shouldn't be using private members inside templates and it's only a coincidence that it works.

My misunderstanding about using private members in the template was because I thought, that template might be evaluated INSIDE the scope of Component class. Now I checked the output of the Angular's compiler and can confirm that Component's members used in the template are accessed from OUTSIDE of the Component class' scope . Which makes your point about potential breakage of the code 100% valid.

It was very logical and appealing to use private for members relevant for component's templates only and public for members exposed as public API of the component... The truth is a bit disappointing.

Best,
Yaroslav.
Reply all
Reply to author
Forward
0 new messages