1

just saw 2 different demos and they differ some in the code. What is the reason for the two different approaches?

app.controller('AppCtrl', ['$scope', '$mdDialog', function($scope, $mdDialog){
  //Code goes here
}]);

and

app.controller('AppCtrl', function($scope, $mdDialog){
  //Code goes here
});

As I understand it the first one is to recommend in case of I'm to use minification.. but is there other reasons to consider?

1
  • It expands upon the minification use case, but when you intend your code to be re-usable as a library / module, the annotated version (so the first) is considered the best practice. Commented Mar 23, 2015 at 16:05

2 Answers 2

2

The first syntax is minification-safe.

If you minify the second syntax, you will get for your controller :

function(a,b){...};

And you will then get errors.

Note that you can use build plugins like ng-annotate that will transform your code from the second syntax to the first, thus will make your code minification-safe. That's why it is more convenient to go with the second syntax.

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

Comments

2

Both are ways to use Dependency injection, you can read angular's documentation on that matter.

Basically, on the options you just mentioned:

  1. Option 1 - Inline Array Annotation - Here we pass an array whose elements consist of a list of strings (the names of the dependencies) followed by the function itself.

  2. Option 2 - Implicit Annotation - you assume that the function parameter names are the names of the dependencies

Option 1 is safe to minify. See related post - Why implicit annotation are not safe to minify

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.