javascript - Angularjs inject provider into module factory function by string name for minification

ID : 274535

viewed : 24

Tags : javascriptangularjsjavascript





Top 4 Answer for javascript - Angularjs inject provider into module factory function by string name for minification

vote vote

97

Try

appModule.factory('sharedApplication', ['$rootScope','$http',function($rootScope, $http) {  }]); 

regards

vote vote

89

ng-annotate is also a good library so that the required dependency is injected automatically. You should check it out.

Code sample:

/* ngInject */ appModule.factory('sharedApplication', function($rootScope, $http) {   var sharedApp;   sharedApp = {};   sharedApp.currentView = "home-section";   sharedApp.pastEvents = null;   $http.get('api/highlights').success(function(response) {     return sharedApp.pastEvents = response.data;   });   return sharedApp; }); 

Then you don't have to write:

appModule.factory('sharedApplication', ['$rootScope','$http',  function($rootScope, $http) {  var sharedApp;  sharedApp = {};  sharedApp.currentView = "home-section";  sharedApp.pastEvents = null;  $http.get('api/highlights').success(function(response) {  return sharedApp.pastEvents = response.data; });  return sharedApp; }]); 
vote vote

72

The more elegant and easier to read approach:

appModule.factory('myFactory', myFactory);  myFactory.$inject = ['$rootScope','$http']; function myFactory($rootScope, $http) {     ... } 
vote vote

70

Top 3 video Explaining javascript - Angularjs inject provider into module factory function by string name for minification







Related QUESTION?