Surprisingly enough, if you write your directive controllers as per the documentation, they will break when minified.
IMO the standard should be to write minification compatible code. The docs do talk about using the minfication-compatible square-brakets [] DI syntax for controllers and such, but not for directive controllers. In fact every directive I’ve ever seen in videos, at meetups or in docs will break when minified.
As per my testing below, square brackets are required for directive controllers too. This is how you do it:
Documented way (breaks)
var myModule = angular.module('myModule', []); myModule.directive('breaksWhenMinified', ['anyService', function(anyService) { return { restrict: 'A', controller: function($scope, $attrs, $element) { alert($scope.whatever); } });
Notice that it doesn’t matter if you use square brackets at the directive level, the minifier still destroys the parameter names of the controller function.
EDIT: Michael has noted in the comments below that anywhere AngularJS expects a function, it can also take an array. We will use the array syntax here because it can be minified. NB: even though the link function takes specially named parameters, it doesn’t require the array syntax!
Undocumented way (works)
var myModule = angular.module('myModule', []); myModule.directive('breaksWhenMinified', ['anyService', function(anyService) { return { restrict: 'A', controller: ['$scope', '$attrs', '$element', function($scope, $attrs, $element) { alert($scope.whatever); }] }; });
I don’t understand why the docs teach you to write code that is incompatible with minification. Even in the videos, people are writing code that will break when minified. You have been warned!
There’s a better way. Anywhere Angular expects a function it will take an array as well. The array can be the usual list of dependencies and a function.
Here’s a working version: http://jsbin.com/onevaz/1/
For what it’s worth, I agree that the docs need to reflect this.
Damn that’s awesome, thanks for letting me know!
Thanks! Why don’t you add this to the angular docs? I’d do it but I don’t want to steal your credit. People would definitely benefit from this.
Thanks! I was banging my head as well, I had _everything_ correctly written to handle the minification since the beginning but for a simple controller inside a directive…