Answer the question
In order to leave comments, you need to log in
When is it better to use injector and when to just enumerate as a function argument?
in what situations that is
.controller('someCtrl', function($scope,mainService) {
mainService.method().....
})
.controller('someCtrl', function($scope) {
var myInjector = angular.injector(['app']);
var service = myInjector.get('mainService');
service.method()...
})
Answer the question
In order to leave comments, you need to log in
so, let's start with the fact that there is a $injector service:
function SomeController($injector) {
var service = $injector.get('mainService');
}
angular.controller('SomeController', ['$injector', SomeController]);
Sergey Protko At the last ngConf there was an example of a controller as an ES6 class, as a preparation for the transition to the 2nd version. In this case, do you propose to shove all used services in this in the constructor? I just save the injector and get everything I need from it along the way.
Essentially, the direct use of the $injector service is the service locatior.
On the Internet, they write a lot, they say this is an anti-pattern, they say it’s bad.
I don't see any reason not to use dependency injection.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question