Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have the following code:

appModule = angular.module('appModule', []);

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;
});

This code works perfectly and as expected until I try to minify my javascript and then I get

    Error: Unknown provider: eProvider <- e

This is because the $http argument in my factory function has been renamed to 'e' for minification purposes. So how can I manually inform the appModule what to inject by name to avoid minification breaking my code?

Thanks in advance.

like image 982
bitwit Avatar asked Sep 27 '12 21:09

bitwit


People also ask

How do I inject a module in AngularJS?

Injecting a value into an AngularJS controller function is done simply by adding a parameter with the same name as the value (the first parameter passed to the value() function when the value is defined). Here is an example: var myModule = angular. module("myModule", []); myModule.

What is $inject in AngularJS?

Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider.

Which component can be injected as a dependency in AngularJS?

The "Application Module" can be injected as a dependency in AngularJS.

Which components Cannot be injected as a dependency in AngularJS?

Note that you cannot inject "providers" into run blocks. The config method accepts a function, which can be injected with "providers" and "constants" as dependencies. Note that you cannot inject "services" or "values" into configuration.


1 Answers

Try

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

}]);

regards

like image 72
kfis Avatar answered Dec 06 '22 17:12

kfis