Is it necessary to inject $scope to a controller i

2019-03-01 03:05发布

问题:

This question already has an answer here:

  • Reason for using array notation when defining AngularJS Controller 3 answers

Is there any difference between the following two code snippet? Both work.

1.

myApp.controller("myAppController", ["$scope", function($scope) {
// function body
}]);

2.

myApp.controller("myAppController", function($scope) {
// function body
});

回答1:

Well, difference will create during minfication. If you don't follow the step1 , minification will break your code.

Uglify Version of your 1st code

myApp.controller("myAppController",["$scope",function(o){}])

Uglify Version of your 2nd code

myApp.controller("myAppController",function(o){})

If you follow step 1 , Angular will find definition of o from injection.

But if you follow step 2 , Angular won't find definition of o from any source.