0

I saw these two differents patterns to define controllers in AngularJS:

myApp.controller('myControllerName', function($scope) {
    // ... my controller code ...
});

and

myApp.controller('myControllerName', ['$scope', function($scope) {
    // ... my controller code ...
}]);

In dependency injection, parameters added respectively.

What is the difference between those two coding routines? I'd love for detailed answer, for both the JavaScript and AngularJS low-level aspects.

0

3 Answers 3

3

The second way is used to resolve minification issues.

Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

We can overcome this problem by annotating the function with the names of the dependencies, provided as strings, which will not get minified

A Note on Minification

Sign up to request clarification or add additional context in comments.

6 Comments

So, if I have no plans to minify my code, is it right to always use the first way? Does it work with all kinds of dependency injections? Is the desire to write a shorter, more readable code (and less code duplications) legitimate in this context?
@Reflection yep, you can use first pattern if you have no plans to minify your code (you really sure? :)). anyway both methods are correct.
For the sureness: if I'm using NodeJS, isn't minifying the JavaScript code is like minifying PHP code in a conventional website's server-side (something that never happens, as far as I know)?
@Reflection - Presumably your Angular app is being served to the browser. Angular wouldn't make much sense purely on the server. You should minify any script that is served to the browser.
Even if there are no remote HTTP requests? In a host local, or a PhoneGap applications, for instance. Clearly there is some overhead, but is minifying really mandatory in these cases?
|
1

Both examples will have the same effect. When using the first pattern Angular parses out the parameter names by converting the function to a string. When using the second pattern the names are explicitly provided as strings.

The second pattern is useful because it's good practice to minify production code. Any good minifier will rename parameter identifiers so the first example would end up something like this:

a.controller('myControllerName',function(a){});

In this situation Angular will no longer know which service to inject since it will parse the parameter names, get a and attempt to inject the a service (which may or may not exist but definitely isn't what you wanted).

The second example will minfiy to something like this:

a.controller('myControllerName',['$scope',function(a){}]);

This time Angular will provide the correct service. Inside the controller function a is a reference to the provided service and everything will work as expected.

2 Comments

Thanks. Please see my comment to Artem Petrosian's answer.
Yes, you can use the first pattern. There are various tools (such as Grunt ngmin) that you can use to automate the conversion to the array pattern at build time anyway should minification become a future requirement.
0

Both will do the same thing, but the later is useful when we minify code.

Minifying code will shorten local variables and arguments names in functions. For example, your code snippets would be changed to the following after minification:

myApp.controller('myControllerName', function(a) {

});

and

myApp.controller('myControllerName', ['$scope', function(a) {
    // ... my controller code ...
}]);

As angular inject services based on the argument name, it would not find the provider with the name 'a', so it fails.

Comments

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.