2

constructor(srv: SomeService)

constructor(private srv: SomeService)

constructor(public srv: SomeService)

What is the difference between these DI's and which one should I prefer?

1 Answer 1

6

That's not related to Angular DI, that's only related to TS class constructors.

The later 2 implicitly create a class field named srv while the first does not. In the first example srv will only be available within the constructor body.

Sign up to request clarification or add additional context in comments.

2 Comments

So, is there any difference between the last two (private vs public for the injected srv)?
No. When one of these is present, then a field with the same name will be created and the value automatically assigned to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.