4

What is the recommended access modifier for the ng2 properties and methods that are to be used exclusively from the view?

I have been using private, but run across this post, that discourages it: Angular2 - should private variables be accessible in the template?

At the same time, marking everything as public doesn't seem right, and neither does protected.

amy8374
  • 1,450
  • 3
  • 17
  • 26
  • It's a good question. Please share your views after reading all the answers what you did at the last. – Tanzeel May 13 '20 at 14:57

2 Answers2

1

At first it would seem like Public would not be desired because that would mean it would be accessible to anything. But the thing is, they aren't really.

It does not make sense to ever instantiate a component to call its properties and methods. Rather, the purpose of a component's class is to support the template.

If you have a child component, you get at its properties using @Input.

You could use @ViewChild or @ViewChildren to get a child component and access its properties or methods. I just tried this and indeed public properties are accessible and private properties are not. But this is the only scenario I could think of.

Is there another scenario you are thinking of?

DeborahK
  • 57,520
  • 12
  • 104
  • 129
  • Thanks for the answer. I have a field that is used by the view to show/hide elements, and its value is set by the setter of the @Input. I dislike making this property public, when its value is supposed to be exclusively set by this setter. I guess for code purity, i can continue with the setters/getters affair for this property as well. – amy8374 Sep 05 '17 at 22:21
  • @DeborahK, Can you please answer this. I'm asking from unit testing point of view. Let's say I've an event handler `OnToggleSwitchChange()`. This will be used in it's template but not by any other component. What should be its access mode? public or private. Or else to be on the same page you can take any of your example on pluralsight and then explain (i follow all your lectures by the way :-) ). – Tanzeel May 13 '20 at 15:08
  • When a method is private, it can't even be accessed by its template, only other code in the component. So the majority of the methods in the component will need to be public for the template to access them.And with the new default compiler in Angular v9, private methods accessed by the template will generate a syntax error. – DeborahK May 19 '20 at 14:55
0

Currently, 'public' is probably ideal, though 'protected' suppresses an ALS warning message but should be avoided if you rely on AOT compilation.

The Angular Language Service (which may be used by your IDE to highlight errors/warnings in your code) issues the warning: "Identifier '' refers to a private member of the component" when using private as the access modifier for a member.

A stackoverflow answer for a similar question on this topic involving the warning message also points to a related access/AOT git hub issue discussion that may explain why the warning exists and speaks to 'public' vs. 'private' in AOT and non-AOT as well as subtleties between access modifiers in TypeScript vs JavaScript.

I've had success in avoiding the warning in my IDE while using 'protected' as long as I'm not concerned with Ahead-of-Time (AOT) compilation, but I've never seen this recommended elsewhere.

jmmygoggle
  • 921
  • 7
  • 18