1

I'm trying to identify if I understand some core concepts in AngularJS. If I have a line of code that says:

.controller('MyCtrl', function($scope) { 

and I change it to:

.controller('MyCtrl', function($scope, $myService) { 

Is it correct to say that "$myService was injected into MyCtrl". Or is it more correct to say that "$myService is injected as a dependency of MyCtrl"? Or, should it be said some other way entirely?

Thank you so much!

3 Answers 3

1

Either are fine. In your example, myService was injected into MyCtrl as a dependency of MyCtrl.

One note, though. You shouldn't name your services with a dollar sign at the beginning as that is reserved for providers within the Angular source code.

Instead, do:

.controller('MyCtrl', function($scope, MyService) { 
Sign up to request clarification or add additional context in comments.

Comments

0

It is correct to say that you inject service in a controller as you would be using service methods in that controller, either as a data transfer across multiple controllers or by getting it via server.

It is recommended to inject service using this notation

.controller('MyCtrl', ['$scope','$myService',function($scope, $myService) {}]);

As when you minify your controller js then it will give no errors in this format.

Comments

0

A service is a way to maintain common state between several controllers, so yes, injecting a service into a controller is entirely correct.

Docs

Comments

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.