angularjs controller的两种写法

在Angular中,Directive、Service、Filter、Controller都是以工厂方法的方式给出,而工厂方法的参数名对应着该工厂方法依赖的Service。如:

app.controller('wolrdCtrl', function($scope, $http){
    // ...
});

在上述的function执行之前,Angular Injector会生成一个$scope的实例和$http的实例,并传入该方法。 如果你希望对JS进行压缩处理,那么参数名就可能发生变化,Angular Injector将不能够正确地注入依赖的Service。于是有另外一种写法:

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

以字符串数组的形式来声明依赖项,因为字符串常量不会被压缩。

原文地址:https://www.cnblogs.com/feng18/p/5137202.html