1

The angular docs say that it supports these two forms of DI setup (plus another, but it's not necessary for this question):

with

var app = angular.module('MyApp', []);

you can

1. app.controller('MainCtrl', function ($scope, $http) { } );

or

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

I have 2 questions about this.

  1. First, I assume they can do the first form of DI by doing function.toString and parsing the string of the function to learn that $scope and $http are the required dependencies. Is that a correct assumption?

  2. Second, it would seem to me that if you use the array form (the one I show 2nd), then Angular wouldn't have to do the string parsing (assuming that is what's happening), and would increase performance (especially depending on how many controllers it's parsing). But I never hear anyone talk about how it is a performance gain. Is it?

I'm not sure any of my assumptions are correct, or even if the performance gain would be worth the effort I've taken to post these questions, but I'd love to get some details on how it all works from someone more familiar with the source than I am.

Edit: The Angular docs do talk about how toString is used. That answers one of my questions.

1
  • Optimising DI, in my opinion, brings negligible effects, and is quite fruitless endeavour. You should really focus on optimising your XHR calls and application logic (template and resource caching, pagination, re-use, function caching, etc.) Commented Jan 24, 2014 at 11:10

1 Answer 1

1

As far as I know the sole reason of writing parameters as a string array is to prevent any problem that can mess up dependency injection after minification.

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

1 Comment

That reason definitely makes sense, and it's the only one I've ever heard as well. It could be that the controller function interacts the same way with the injector regardless of how you declare it.

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.