0

What is the difference between defining a controller like this

app.controller('MyCtrl', ['$scope', '$http',    
function ($scope, $http) {
    //...
}
]);

or this?

app.controller('MyCtrl', function ($scope, $http) {
    //...
});
1
  • The second won't minify correctly. Commented Oct 27, 2014 at 20:10

3 Answers 3

2

They both work for he same, but the array notation will survive minification.

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

1 Comment

Which (just in case) is the first option.
0

Fist option is 'safe for minification'.

For more info please see here https://docs.angularjs.org/tutorial/step_05#a-note-on-minification

Comments

0

the difference is that when code is magnified the arguments passed should not be changed to other names and further code becomes in correct as we may have constructors injected into the function and other useful stuff , so in order to avoid we take arguments as array items i.e in string so they when magnified matches the arguments.

i have a good quote from a person

When you minify JavaScript the JavaScript minifier replaces the names of local variables and parameters with shorter names. However, AngularJS uses the parameter names of controller functions, factories, services and providers to decide what to inject into their factory functions. If the names are changed, AngularJS cannot inject the correct objects.

To make your AngularJS code minification safe, you need to provide the names of the objects to inject as strings. You wrap these strings in an array together with the function that needs the values injected. Here is an AngularJS minification safe dependency injection

your own example:

  app.controller('MyCtrl', ['$scope', '$http',    
  function ($scope, $http) {
   //...
   }
  ]);

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.