reknirt
reknirt

Reputation: 2254

Why place $scope in the dependencies array on a controller?

I've seen a lot of people, including the Angular docs, declare controllers like so:

app.controller('ExampleController', [$scope, $http, function($scope, $http) {
    $scope.name = "Bob";
}]);

Why do we need to put $scope in the dependency array? Is this the syntax of an older version of Angular? Can we instead write it like:

app.controller('ExampleController', [$http, function($scope, $http) {
    $scope.name = "Bob";
}]);

Is there a difference between the two? Thank you.

Upvotes: 2

Views: 84

Answers (1)

Mathew Berg
Mathew Berg

Reputation: 28750

What you've been looking at is actually something like this:

app.controller('ExampleController', ["$scope", "$http", function($scope, $http) {
    $scope.name = "Bob";
}]);

The reason this is done is to deal with minification. When minifying the $scope, $http would be crushed into variables like a and b since dependency injection doesn't work through minification. Angulars solution to this is to make the controller an array where you can pass in strings.

See: https://docs.angularjs.org/tutorial/step_05#-prefix-naming-convention

Upvotes: 2

Related Questions