javascript - 如何防止 grunt-uglify 混淆指令中的 angularJS 范围值

标签 javascript angularjs gruntjs grunt-contrib-uglify

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

这是指令的一个片段:

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 将更改为:controller: function(c) {}

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

亲切的问候

最佳答案

你也必须注释 Controller 函数:

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

关于javascript - 如何防止 grunt-uglify 混淆指令中的 angularJS 范围值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31557671/

相关文章:

javascript - 如何将回调函数传递给 angularJS Controller 中的 $compile 服务?

javascript - 为什么当我使用输入类型数字并且我也使用正则表达式模式 ="[0-9\/]*"时 e 字符正在工作任何解决方案

django - 在 Grunt 中启动 Django 虚拟环境

javascript - 将 grunt 与现有的 wordpress 实例一起使用

css - Grunt compass 和 sass/css 目录

javascript - 如何让日期选择器设置默认年份?

javascript - 使用 JavaScript 的范围选择器

javascript - 将字符串中的特定单词更改为粗体

javascript - 在 Javascript 中访问属性

javascript - 从 firebase 数据库检索数据时出现问题