TechQuery
TechQuery

Reputation: 253

Angular js Injection Clarification

I m new to Angular JS and I was going through few angular JS tutorials. The below one is one of the controller method. In the first case, $scope is included as part of the array string but in the second case $scope is injected in the function alone but both works fine. What is the difference and which approach has to be used?

 app.controller('myController', ['$scope', function ($scope) {
            $scope.message = "Test Success";
        }]);

        app.controller('myController', function ($scope) {
            $scope.message = "Test Success";
        });

Upvotes: 1

Views: 57

Answers (3)

Dan Moldovan
Dan Moldovan

Reputation: 3591

Both notations are equal in the eyes of angular. I personally prefer and recommend the first one even.

Even though the above comments are correct, minification will break this, there is still ngAnnotate which is built specifically for this: Using the second notation it converts it to the array notation when minifying

Upvotes: 1

Umesh Kolapkar
Umesh Kolapkar

Reputation: 22

First approach is preferable because while minification of javascript function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

Upvotes: 1

Shehryar Abbasi
Shehryar Abbasi

Reputation: 378

the first method is the recommended practice so the app does not break when (or if) minified.
give this a read: https://scotch.io/tutorials/declaring-angularjs-modules-for-minification
and this: https://docs.angularjs.org/tutorial/step_05 (scroll down to "A Note on Minification")

Upvotes: 1

Related Questions