16
Considering the injection of dependencies into Angularjs, there are some ways to do it. The modes as far as I know are:
Form 1:
angular
.module('meuModulo', [])
.controller('MeuController', function(dependencia)) {
//...
});
Form 2:
angular
.module('meuModulo', [])
.controller('MeuController', ['dependencia', function(dependencia)) {
//...
}]);
Form 3:
angular
.module('meuModulo', [])
.controller('MeuController', MeuController);
MeuController.$inject = ['dependencia'];
function MeuController(dependencia)) {
//...
}
I used as an example the controller
but can use to factory
, directive
, filter
, etc..
My questions about this are: What are the real differences between the ways to inject dependencies? What is the indication for each case? There are other ways to accomplish them?
I always use the
forma 2
+1 for the question ...– novic
Your 3rd option seems more suitable with the standards of good practice. I recommend giving a read on the guide of John Pope, helped me a lot! https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md I hope I helped. Hug
– Rafael B. Marcílio