Using $injector vs. direct DI in angularjs? -
i not seeing difference (or pros/cons) directly injecting dependencies vs. using $injector in angularjs. why 1 or other? here sample of two.
angular .module("myapp") .factory("myservice", [ "$injector", function($injector) { var $http = $injector.get("$http"); //used $injector $http, data, etc. } ]); angular .module("myapp") .factory("myservice", [ "$http", function($http) { //just use $http data, etc. } ]);
in cases, second method simple, readable , works, if use precompiler writes string values you, e.g. ng-annotate:
angular.module("myapp") .factory("myservice", /*@nginject*/ function($http, $locale, $window) { });
i see no reason why should avoid it.
the first method, using $injector
should reserved specific cases, e.g. when there many injections (and don't want refactor right now) or in specific cases in tests.
Comments
Post a Comment