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

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -