moritz.muecke
moritz.muecke

Reputation: 167

Difference of function($scope) and ['$scope', function($scope)] in AngularJS

I am new to AngularJS. I try to find out whats the difference between these two controller definitions:

app.controller('simpleController', ['$scope', function($scope) {
}]);

app.controller('simpleController', function($scope) {
});

I always use the second example, but sometimes I see people using the first example. Why should I do that? Is the controller in the first example inheriting another $scope variable?

Upvotes: 2

Views: 160

Answers (2)

influxd
influxd

Reputation: 536

Those two controller definitions do the exact same thing. In the first definition, you're explicitly telling Angular the name of the dependency through the use of a string. This allows you to minify your code, since minifiers do not change the contents of strings.

In the second definition, Angular infers what dependency to inject by looking at the parameter name, and thus minifying this code will break it.

Upvotes: 1

Nisham Mahsin
Nisham Mahsin

Reputation: 1399

The first example

app.controller('simpleController', ['$scope', function($scope) {
}]);

lets you minify your code

minifer converts $scope to variable a.but its identity is still preserved in the strings. so use first example if you would like to minify your code later.

Upvotes: 2

Related Questions