如何防止 grunt-uglify 弄乱指令中的 angularJS 范围值

How to prevent grunt-uglify to mess with angularJS scope value in directive

我编写了一个相当简单的指令,可以动态更改页面上的样式表。

这是指令的一个片段:

OfficeSuiteModule.directive('officeButton', ['$q', 'stylesheetInjectorService', function($q, stylesheetInjectorService) {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            isDisabled: '@?',
            label: '@?',
            api: '='
        },
        template: OFFICE_BUTTON_TEMPLATE,

        // Defines the controller for the 'officeButton' directive.
        controller: function($scope) { }
    }
}]);

现在,我正在使用 grunt 来构建我的项目,我正在使用任务 grunt-contrib-uglify 来缩小 JavaScript 文件,但是我在这里遇到了一个问题。

如果我查看 JavaScript 文件的缩小版本,我的指令签名中的控制器更改为:controller: function(c) {}

因为 c 未定义,所以这将不起作用。它会引发 AngularJS 错误。 有没有 Angular 方法来解决这个问题,或者我可以指示 grunt-contrib-uglify 任务不要更改此参数?

亲切的问候

你也必须注释控制器功能:

controller: ['$scope', function($scope) {
        // your function
}]

因此您的完整代码变为:

OfficeSuiteModule.directive('officeButton', ['$q', 'stylesheetInjectorService', function($q, stylesheetInjectorService) {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            isDisabled: '@?',
            label: '@?',
            api: '='
        },
        template: OFFICE_BUTTON_TEMPLATE,

        // Defines the controller for the 'officeButton' directive.
        controller: ['$scope', function($scope) {
            // your function
        }]
    }
}]);