0

I have the following question that I can't find what way is better. When doing dependency injection on AngularJS, what is the difference between these alternatives:

app.controller(‘controllerName’, [‘service1’, ‘service2’, function(service1, service2){…}])

Or

app.controller(‘controllerName’, function(service1, service2){…})

I think that the second one is much better because it does not inject the dependency twice, but does have any negative situation? Is the first one contribute with something that the second one not?

1 Answer 1

1

The first method is actually the preferred method of injecting the dependencies. It makes your code safe from code minifyers and obfuscators as those variables youre injecting will get renamed but the string values will not. You can read more about it over at the official documentation, spepcifically the Implicit Annotation section

Also to be clear, the first method only injects the dependency once.

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

1 Comment

Thank you, I found that link but I didn't find the exact answer to my doubt. Now I researched again and found out the difference.

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.