简体   繁体   English

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

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

I've written a fairly simple directive that can change the stylesheet on a page dynamically. 我写了一个相当简单的指令,可以动态更改页面上的样式表。

Here's a snippet of the 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) { }
    }
}]);

Now, I'm using grunt to build my project and I'm using the task grunt-contrib-uglify to minifies the JavaScript files, however I'm facing an issue here. 现在,我正在使用grunt来构建项目,并且正在使用grunt-contrib-uglify uglify任务来最小化JavaScript文件,但是在这里我遇到了一个问题。

If I look at the minified version of the JavaScript file, the controller inside my directive's signature is changed to: controller: function(c) {} 如果我查看JavaScript文件的缩小版本,则我指令的签名内的控制器将更改为: controller: function(c) {}

Off couse this would not work bcause c is not defined. 当然这是行不通的,因为未定义c It would raise an AngularJS error. 它将引发AngularJS错误。 Is there an Angular way to resolve this, or can I instruct the grunt-contrib-uglify task not to change this parameter? 有没有解决此问题的方法,还是可以指示grunt-contrib-uglify任务不要更改此参数?

Kind regards 亲切的问候

You have to annotate the controller function too: 您还必须注释控制器功能:

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

So your full code becomes: 因此,您的完整代码将变为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM