AngularJS转换为迷你友好型

时间:2017-03-10 06:00:09

标签: javascript angularjs angularjs-directive

当我缩小我的js脚本时,我发现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;
    })
}

这似乎是一个缩小问题,它重命名变量。虽然我不确定如何将其转换为对迷你友好。

2 个答案:

答案 0 :(得分:2)

你需要向装饰者注入$delegete。你可以用两种方式做到这一点。

一个是创建单独的函数并为此函数注入服务。

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

选项2

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

        return $delegate;
    }])
}

答案 1 :(得分:1)

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

             return $delegate;
         }])
     }