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){
    // ...
}]);

 

 

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

 

posted @ 2016-01-17 14:29  阿锋佬  阅读(255)  评论(0编辑  收藏  举报