1

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.

If I look at the minified version of the JavaScript file, the controller inside my directive's signature is changed to: controller: function(c) {}

Off couse this would not work bcause c is not defined. It would raise an AngularJS error. Is there an Angular way to resolve this, or can I instruct the grunt-contrib-uglify task not to change this parameter?

Kind regards

1 Answer 1

4

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
        }]
    }
}]);
Sign up to request clarification or add additional context in comments.

6 Comments

That was exactely the answer I was looking for. Didn't know it was that simple. Thanks for that. You have to wait 10 more minutes before you are awarded SO points, cannot accept for the next 10 minutes.
@Complexity I would like to also add that you could pull it out to its own controller file and keep everything encapsulated but I'm not sure if this is just a simple directive or it will grow in the future. You can use the [ ] min-safe array approach or $inject if you pull it out to its own file.
I suggest you to use this plugin github.com/mzgol/grunt-ng-annotate. It'll auto annotate the files.
@TheLazyChap I didn't know that you can pull a directive's controller out if's directive. You mind giving a short example as I might need it in the future?
@Complexity I've created a plunk for you here plnkr.co/edit/l8YiIpF9k2JzMmYk3N4x?p=preview have a look and see if it make sense. I've added some comments in the directive's controller file. It should be self explanatory.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.