1

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";
        });

3 Answers 3

1

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")

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

Comments

1

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.

Comments

1

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

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.