I am currently utilizing Angular2 with Typescript
Imagine having the following code in the template of my app component:
...
<coffee-cup [coffee]=""
...
Here is my coffee-cup
component:
@Component({
selector: 'coffee-cup',
...
})
export class CoffeeCup {
@Input()
public coffee = 0;
}
At this moment, I am uncertain about how to define my Input. It could be like this:
@Input()
public coffee = 0;
Or
@Input()
private coffee = 0;
Currently, I am inclining towards making the member variable coffee private.
- I aim to establish a clear public API for the component
- I prefer to only allow setting the coffee property through the template
- Currently, there is no necessity for coffee to be read or set directly from the parent component. If required, I can remove the private modifier.
In my perspective, a component has two distinct APIs for interaction:
- The template API, which includes the
@Inputs
and@Outputs
- The Typescript API encompassing all public properties and methods
I have not yet investigated the outcome in the following scenario, it might alter the response:
- Assume the coffee member is public. If my
appComponent
accessesCoffeeCup
using@ViewChild
and sets the coffee member, will the lifecycle hooks (likengOnChange
) activate?
To reiterate the query: Should Angular2 @Input
s remain public or can/should we enforce a more restricted API by making them private?