缩小后AngularJS指令失败

Gee*_*ang 0 angularjs bundling-and-minification

我在Visual Studio中使用了一个Minifier插件,除了这一段AngularJS代码外,该插件大部分都可以工作

这是未缩小的代码:

var svgBuildInterface = angular.module("svgBuildInterface", []);

svgBuildInterface.directive('ngRightClick', function ($parse) {
    return function (scope, element, attrs) {
        var fn = $parse(attrs.ngRightClick);
        element.bind('contextmenu', function (event) {
            scope.$apply(function () {
                event.preventDefault();
                fn(scope, { $event: event });
            });
        });
    };
});
Run Code Online (Sandbox Code Playgroud)

这是打印精美的缩小的代码,但失败了:

svgBuildInterface = angular.module("svgBuildInterface", []);
svgBuildInterface.directive("ngRightClick", function(n) {
    return function(t, i, r) {
        var u = n(r.ngRightClick);
        i.bind("contextmenu", function(n) {
            t.$apply(function() {
                n.preventDefault();
                u(t, {
                    $event: n
                })
            })
        })
    }
});
Run Code Online (Sandbox Code Playgroud)

我无法在缩小的代码中添加断点以了解正在发生的事情,但是angularJS会引发异常:

Error: [$injector:unpr] http://errors.angularjs.org/1.5.7/
$injector/unpr?p0=nProvider%20%3C-%20n%20%3C-%20ngRightClickDirective
Run Code Online (Sandbox Code Playgroud)

ngC*_*der 5

如下更改您的指令,当您要缩小JS时,在编写控制器或指令或Angular js的任何组件时要遵循某些最佳实践。

其中之一是通过[]通过依赖项注入

var svgBuildInterface = angular.module("svgBuildInterface", []);

svgBuildInterface.directive('ngRightClick',['$parse', function ($parse) {
    return function (scope, element, attrs) {
        var fn = $parse(attrs.ngRightClick);
        element.bind('contextmenu', function (event) {
            scope.$apply(function () {
                event.preventDefault();
                fn(scope, { $event: event });
            });
        });
    };
}]);
Run Code Online (Sandbox Code Playgroud)