2

I learnt to write angular dependencies needed using the array notation, that way:

var app = angular.module('MyApp', []);
app.controller('MyCtrl', ['$scope', function($scope) {
    $scope.stuff = 'stuff';
}]);

The Angular doc follows this notation, but I see more and more tutorials not using the array notation and just directly passing the controller the function($scope).

Is there any differences between the two ways to do? Or maybe one was implemented in the version two?

1
  • 3
    The difference is that the function($scope) method breaks when you minify. Commented Aug 19, 2015 at 18:05

8 Answers 8

4

You should be using the Array notation

say Tomorrow if you wish to minify your data using a uglify say, it minifies your big variable names but doesn't touch your strings so your statement from

Case 1 with array notation

original

app.controller('MyCtrl', ['$scope', function($scope) {

minified

x.controller('MyCtrl', ['$scope', function(a) {

Here controller knows exactly knows that variable a is $scope

Case 2 without array notation (whereas if you choose not to use it)

original

app.controller('MyCtrl', function($scope) {

minified

x.controller('MyCtrl', function(a){

Now your controller doesn't know what to do with a variable its not $scope for sure

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

Comments

1

The array notation is important if you plan to minify your code, which you should be doing in production anyway. Stick to using it.

1 Comment

I'm using it already. But when I want to include a factory I made in the array then as one of the function parameters as I learnt it, the controller doesn't start, it seems that I should only write it as a function parameter
1

If you're planning to mini your app, yes you MUST use the array notation.

This is because the variables are renamed, so the injector no longer knows what dependencies you're intending to inject.

For example if $scope was renamed a on minification it wouldn't work.

Obviously this means you have to write and maintain more code. Luckily, you can automate this in your build process.

On my project I use grunt and angular-templates.

https://www.npmjs.com/package/grunt-angular-templates

Comments

0

Yes there is a difference. I would recommend continuing to use array notation since not using it will break your dependency references if the application is minified. For more details, read https://docs.angularjs.org/tutorial/step_05 .

Comments

0

Basically you can rename the variables without affecting angulars ability to inject. As others have said, specifically for minification.

var app = angular.module('MyApp', []);
app.controller('MyCtrl', ['$scope', function(s) {
    s.stuff = 'stuff';
}]);

1 Comment

I didn't get it. Should I rename the $scope variable to s inside my function or will angular know that s is $scope? If it knows anyways, do you recommend me to change it to the minified version inside my function?
0

Array notation is used for injecting the dependencies. Difference between this two is if u do not include array notation means u do not need any dependencies.

Best practice is to use [] notation so that u can include dependencies any time in you application.

Comments

0

In a nutshell there are two ways how you can use dependency injection in angular: implicit or explicit.

Implicit DI is .controller('MyCtrl', function(scope, dep1, dep2){

It does not give instructions to $injector on what to include, it bvasically doing something like a reflection to figure it out. If you minify your code it will turn into

`.controller('MyCtrl', function(a, b, c){ `

That is why we are using explicit DI. There are several ways to do so:

  • use array : .controller('MyCtrl', ['$scope','dep1','dep2', function($scope, dep1, dep2){

  • use $inject:

    .controller('MyCtrl', myCtrl)
    
    myCtrl.$inject = ['$scope', 'dep1', 'dep2'] 
    
    function myCtrl($scope, dep1, dep2) {}  
    

In both ways, even if minification renamed function parameters to something, $injector will know what it is expected to inject, as it has original names in the string literals ( which are not affected by minification)

You can also use comment annotations that will tell compiler/transpiler how to handle angular DI , ie it will turn implicit DI into explicit for you, see ng-annotate

I personally prefer second way with .$inject

Comments

0

DI helps you while minifying the code, Also other answer does explained how both the DI injection techniques work. Basically none of DI injection technique is bad.

But I'll prefer to do not worry about this thing because other will take care of this DI thing. Use ng-annotate directive, that will give you facility to use dependency inside a function directly

If you wrote code like this

angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) {
   //awesome code here
});

When you send this code to minifier it does add the array annotation of dependency on the angular component while

angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", 
  function($scope, $timeout) {
    //code here
  }
]);

From Docs

ng-annotate works by using static analysis to identify common code patterns. There are patterns it does not and never will understand and for those you can use an explicit ngInject annotation instead, see section further down.

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.