11

I used Gulp to minify my entire js files. Once minified I got an error like the following:

[$injector:unpr] Unknown provider: eProvider <- e <- makeErrorsDirective.

I had a Custom directive in my controller file.

var myhubdashboardControllers = angular.module('vpdashboardmodule', []);

.directive('mhDashboard', function ($http, authService, apiService) {
    return {
        restrict: 'EA',
        scope: {
            name: '@',
            dash: '@',
            report: '@',
            disname: '@',
            disdesc: '@',
            distot: '@'
        },
        templateUrl: 'views/dashboard/dashboard-direc.html',
        link: function (scope, element, attr) {
            scope.linkChk = scope.name;
            switch (scope.linkChk) {
                case 'Shipped This Week':
                    scope.url = 'erp/JobShipmentList/PostCpsVwShipmentCount';
                    scope.shipstatus = "Departure";
                    scope.period = "ThisWeek";
                    scope.basicfilter = "Open";
                    scope.linkName = "Shipments";
                    scope.linkDesc = "Shipped This Week";
                    break;

}) };

This is the code used in my application.

Dhamodharaan M
  • 123
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [Angularjs minification using grunt uglify resulting in js error](http://stackoverflow.com/questions/22866183/angularjs-minification-using-grunt-uglify-resulting-in-js-error) – Webbies Feb 01 '16 at 09:35

4 Answers4

23

There is a reason why you have to inject services and controller in string array.

if you want to inject scope to controller, you have to use

angular.module('yourApp')
    .controller('yourController',['$scope',function($scope){
    }]);

Minification will change the variable names and if you don't use that array of strings while injecting services or controllers, it will be like

 angular.module('yourApp')
    .controller('yourController',function(e){
    });

So, angular will not be able to understand what 'e' stands for, hence the error. Always remember that the order is also important.

.directive('mhDashboard', ['$http','authService','apiService', function ($http, authService, apiService) {
    return {
        restrict: 'EA',
        scope: {
            name: '@',
            dash: '@',
            report: '@',
            disname: '@',
            disdesc: '@',
            distot: '@'
        },
        templateUrl: 'views/dashboard/dashboard-direc.html',
        link: function (scope, element, attr) {
            scope.linkChk = scope.name;
            switch (scope.linkChk) {
                case 'Shipped This Week':
                    scope.url = 'erp/JobShipmentList/PostCpsVwShipmentCount';
                    scope.shipstatus = "Departure";
                    scope.period = "ThisWeek";
                    scope.basicfilter = "Open";
                    scope.linkName = "Shipments";
                    scope.linkDesc = "Shipped This Week";
                    break;
}
}])
Raghu Venmarathoor
  • 858
  • 11
  • 28
  • i had a custom directive in my controller file.after minification it produces the error like [$injector:unpr] Unknown provider: eProvider <- e <- e directive..Other than that everything working fine. – Dhamodharaan M Feb 01 '16 at 09:51
  • 1
    thanks Raghu. I done for controller but i missed for directives. It works now. – Dhamodharaan M Feb 01 '16 at 11:10
  • 2
    Had the same issue, I had to use the `["$http", function ($http) { ... }]` syntax on my services & directives as well as my controllers. Thank you for pointing this out. +1'd – mhodges May 08 '17 at 18:44
2

Angular doesn't always work well with minification.

If you as an example write this:

angular.controller("MyCtrl", function ($scope) {...});

Then the $scope would be changed to something meaningless during minification.

If one instead changes that to:

angular.controller("MyCtrl", ["$scope", function (s) {...}]);

Then it doesn't matter what the first argument in the function is called (here s), as long as the string is "$scope".

See this: https://docs.angularjs.org/tutorial/step_05#a-note-on-minification in the documentation for more details.

If you want more help, you have to post the code in question, not just the error.

Webbies
  • 947
  • 9
  • 17
2

I had this same issue, even when I use gulp-ng-annotate, but it only happens occurs for $stateProvider and ngDialog resolves:

$stateProvider
  .state('orders', {
    url: '/orders',
    templateUrl: 'templates/orders.html',
    controller: 'OrdersController as vm',
    resolve: {
      authenticate: function (Auth) {
        return Auth.getAuthResolve();
      }
    }
  });

Resolve needs to be written like this:

    resolve: {
      authenticate: ['Auth', function (Auth) {
        return Auth.getAuthResolve();
      }]
    }

So it feels that ng-annotate does not inject the array into resolves, but only to controller/service/factory constructors.

Render
  • 2,199
  • 2
  • 16
  • 14
  • You should use ng-anotate everywhere that has an injection even inside resolve (before every function) – Morteza Aug 26 '17 at 06:47
0

I had an issue using angular-ui-router-title. After changing

$titleProvider.documentTitle(function($rootScope) {
    return $rootScope.$title + ' my title';
});

to

$titleProvider.documentTitle(['$rootScope', function($rootScope) {
    return $rootScope.$title + ' my title';
}]);

the error doesn't appear anymore.

iwanuschka
  • 436
  • 1
  • 5
  • 18