1

In order to keep dependency injection working after minification I'm using this style to declare a controller:

myModule.controller('myController', ['$rootScope', 'Service', 
 function($rootScope, Service) {
 $scope.foo = 'bar';
}]);

Let's say you want to add another service. You'd have to add it in 2 places, in the array preceding the controller function, and as a parameter of the controller function itself. My question is, is there a way to make this DRY-er? Could you declare a dependency array and use that to build the outer array and the inner parameters, something like this?:

var dependencies = ['$rootScope', 'Service'];
var myProtoController = function() {
 $scope.foo = 'bar';
};
var myController = dependencies.push(myProtoController.bind.apply(this, [this].concat(dependencies)));
myModule.controller('myController', myController);
asked Sep 5, 2013 at 21:48

2 Answers 2

3

Can you use Ngmin in your tool chain? It will convert your angular functions into the minify-able style you're currently using.

It integrates with grunt and rails, so it should be simple to use in your current pipeline.

answered Sep 5, 2013 at 21:53

Comments

1

Using concat ?

var deps = ['$rootScope', 'Service'];
myModule.controller('myCtrl', deps.concat(function ($scope, $rs, S) {
 $scope.foo = 'bar';
}));
answered Sep 5, 2013 at 21:52

3 Comments

I like it. But there is a syntax error. Your missing a closing paren.
It looks like you still need to write the dependencies twice in this one.
How so? But +1 for ngmin anyway, I just usually try to suggest from scratch-working solutions.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.