我是Angular初学者,在学习中有个关于定义控制器的写法问题。
在刚接触的时候,书上是这样写控制的:
var myApp = angular.module('MyApp', []);
myApp.controller('MyController', function($scope){
// todo...
})
后来在一些网络上的文章,是这样写的:
var myApp = angular.module('MyApp', []);
myApp.controller('MyController', ['$scope', function($scope){
// todo...
}]);
那么问题来了,后者加了个[]
,是个什么意思?这两种写法有什么不同呢?
在练习中发现同样的实现用这两种都可以。
The second way of writing is called
inline-annotation
, see the document dependency injectionThe main purpose of this writing method is to avoid the problem of dependency injection failure due to variable names being replaced during source code compression (uglify).
If you are interested in the implementation, you can look at handwritten dependency injection
is the dependency to be injected. This way of writing is conducive to future code compression