Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript dependency injection public vs private

What is the difference between injecting service with public and private.I see most of examples use private keyword in angular component. Would it have any implications of using public? e.g.

constructor(public carService: CarService) { } 

vs.

constructor(private carService: CarService) { } 
like image 345
Vimal Trivedi Avatar asked Oct 05 '17 23:10

Vimal Trivedi


People also ask

Should services be public or private?

Generally speaking, services should be injected as private. You'd only mark them as public if you want to access the service directly from the template, which leans towards bad practice.

What is the difference between public and private in Angular?

1. Public: All the properties and methods could be accessed everywhere if they are declared as public. 2. Private: The private declared properties and methods can be accessed only within the class definition itself.

Why we use private constructor in Angular?

It is about encapsulation, and when you have a field or method on your component that you want to encapsulate in it, making it clear that it shouldn't be accessed from anywhere else, then you should absolutely make it private : That's what private is for: It signals your intent that whatever you've put it on shouldn't ...


2 Answers

In addition to the prior answer ... anything marked as private cannot be accessed by the component's template either. (Private members can be accessed when using JIT, such as at development time, but not when using AOT, such as for production.)

So in your template, you could only do *ngIf='carService.isValid' if the injected service was marked as public.

But actually, best practice is to wrap any service properties/methods in a component property/method anyway and have the template bind to/call the component's property or method.

Something like this:

   get isValid(): boolean {       return this.carService.isValid;    } 

And then access it like this: *ngIf='isValid'

like image 149
DeborahK Avatar answered Sep 17 '22 13:09

DeborahK


The answer is pretty simple: you have to create private variables when you don't need to use them outside of current class/component, otherwise, you should create public variables. And one more thing: you can also use private variables and give access to them from outside via special functions called getters and setters. For example:

private _customValue: any;  set customValue(newValue: any): void {   this._customValue = newValue; }  get customValue(): any {   return this._customValue; } 

Notice, that _customValue is private, but you can set/get this value from outside the class via operations with customValue:

classInstance.customValue = 'newValue'; console.log(classInstance.customValue); 

Need to say, that set and get keywords before method names are not strongly needed, they are more for clarification.

like image 32
Commercial Suicide Avatar answered Sep 20 '22 13:09

Commercial Suicide