0

The Stackblitz app linked below is an excerpt demonstrating Angular's guide sample showing how parent and child components can communicate. In this example, the parent references the child component via the @ViewChild decorator, and then is able to invoke its methods.

The parent component's start, stop, and the seconds methods are just wrappers for the child's components of the same name.

My question is, why are my child's methods so open without the option to keep them private? In my pasted code, i have already tried modifying the child's method to declare the methods as private, and as you can see, it was to no avail, as the sample code still runs.

Can i make sure i can write up a component such that i can control which of its methods i want to expose to the parent container, and which of them i want to keep inaccessible for the parent?

Digital Ninja
  • 3,415
  • 5
  • 26
  • 51
  • If you read the warning carefully you would notice some for example: in parent, your child function is private and only accessible in child class; on `timer` component(unrelated to your question):`int` is not a valid type it could be `number` or you don't need to declare and let the system infer. Related: https://stackoverflow.com/questions/38468227/private-members-are-accessible-in-angular-2-decorators, https://stackoverflow.com/questions/34574167/angular2-should-private-variables-be-accessible-in-the-template – Mukyuu Apr 01 '19 at 01:33
  • The `private` keyword is only used by TypeScript, JavaScript doesn't have private class fields [yet](https://stackoverflow.com/questions/45626973/microsoft-edge-adds-parameters-after-navigating-to-a-route-on-angular-2). TypeScript just deletes the keyword, so it gives you an error, but the function call still works. – mahalde Apr 01 '19 at 10:29
  • @Flix i think neither Typescript nor Javascript private classes should be relevant here, i think it should be an Angular solution to.. not necessarily call them "private", that was just my best guess.. but, declare a method of a component in a way that a parent component will not be allowed to use it. – Digital Ninja Apr 02 '19 at 20:58

0 Answers0