22

I've followed all the instructions I can find for fixing minification, e.g.

var MyController = function(renamed$scope, renamedGreeter) {
...
}
MyController.$inject = ['$scope', 'greeter'];

and

someModule.factory('greeter', ['$window', function(renamed$window) {
...;
}]);

yet angular refuses to work still. It always throws the error "Unknown provider: eProvider"

Here are my two attempts to get it working... can anyone help?

https://github.com/jemminger/angular-test1

https://github.com/jemminger/angular-test2

They've already had the assets precompiled and development mode is configured to work as production, so you should just be able to "rails s" to see it (not) work.

jemminger
  • 5,133
  • 4
  • 26
  • 47
  • Depending on one's goals, a possible solution is to scale back the minifier to less-desctructive optimizations. In the case of `uglify` you can use the `mangle: false` configuration and still save quite a few kilobytes with whitespace removal. – rymo Sep 24 '15 at 20:49

3 Answers3

60

Found it! They never said to apply the injection fixes to services too... The solution is to change this:

angular.module('itemServices', ['ngResource']).
    factory('Item', function($resource){
      return $resource('items/:item_id.json', {}, {
        query: {method:'GET', params:{ item_id: 'all' }, isArray:true}
      });
    });

to this:

angular.module('itemServices', ['ngResource']).
    factory('Item', ['$resource', function($resource){
      return $resource('items/:item_id.json', {}, {
        query: {method:'GET', params:{ item_id: 'all' }, isArray:true}
      });
    }]);
jemminger
  • 5,133
  • 4
  • 26
  • 47
  • 4
    @Josiah, it works because the dependency injection allows the function to survive js minification. – jemminger Feb 23 '15 at 18:04
  • 1
    I suggest using ng-strict-di (https://docs.angularjs.org/api/ng/directive/ngApp) to detect the issue also in development env – Marco C. Jul 17 '17 at 17:16
14

Remember, to also use DI on controllers within directives. Took me hours... CS example:

wrong:

controller: ($scope) ->
  $scope.closeModal = ->
    ModalService.close()

right:

controller: ["$scope"
  ($scope) ->
    $scope.closeModal = ->
      ModalService.close()
]
cache.zero
  • 366
  • 3
  • 9
6

Make sure to apply the DI pattern to ALL function definitions that require injection within your module. It can be easy to miss one. If you're using a routeProvider, factory, services, etc., they all need to have the DI pattern applied. I ended up deploying multiple times before I caught them all :P

Kevin
  • 150
  • 3
  • 7
  • 'It can be easy to miss one.' Exactly. You must apply the pattern even to the controllers defined in directives! I wasted one wonderful hour searching for this bug. – anonymous Oct 13 '14 at 22:37