0

When I minify my js scripts, I'm having an error on of Unknown provider: eProvider <- e <- myDirective

angular
    .module('myApp', [])
    .run(['$state', run])
    .config(['$provide', config]);

function config($provide) {
    $provide.decorator('myDirective', function($delegate) {
        var directive = $delegate[0];
        directive.controller = 'MyController as vm';

        return $delegate;
    })
}

It seems like a minify issue wherein it renames variables. Though I'm not sure on how to convert this to be minify-friendly.

basagabi
  • 4,900
  • 6
  • 38
  • 84
  • $delegate is minified. So dependency injector will fail to inject the dependency. Check out this answer: http://stackoverflow.com/a/18782380/5640621 – im_tsm Mar 10 '17 at 06:08
  • Possible duplicate of [Reason for using array notation when defining AngularJS Controller](http://stackoverflow.com/questions/30088534/reason-for-using-array-notation-when-defining-angularjs-controller) – Nico Van Belle Mar 10 '17 at 06:19
  • See [AngularJS Developer Guide - Dependency Injection - Implicit Annotation](https://docs.angularjs.org/guide/di#implicit-annotation) for advice about the pitfalls of that method with respect to minification. – georgeawg Mar 10 '17 at 06:22
  • 1
    Possible duplicate of [Angularjs minify best practice](http://stackoverflow.com/questions/18782324/angularjs-minify-best-practice) – georgeawg Mar 10 '17 at 06:24

2 Answers2

2

you need to inject $delegete to decorator. you can do this with 2 ways.

one is to create separate function and inject service to that function like this.

angular
    .module('myApp', [])
    .run(['$state', run])
    .config(['$provide', config]);

function config($provide) {
    $provide.decorator('myDirective', dirFun)
}

dirFun.$inject = ['$delegate'];

function dirFun($delegate) {
    var directive = $delegate[0];
    directive.controller = 'MyController as vm';
    return $delegate;
}

option 2

function config($provide) {
    $provide.decorator('myDirective',['$delegate',function($delegate) {
        var directive = $delegate[0];
        directive.controller = 'MyController as vm';

        return $delegate;
    }])
}
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
1
    function config($provide) {
         $provide.decorator('myDirective' ['$delegate', function($delegate) {
             var directive = $delegate[0];
             directive.controller = 'MyController as vm';

             return $delegate;
         }])
     }
Parth
  • 568
  • 2
  • 6
  • 23