码迷,mamicode.com
首页 > Web开发 > 详细

angularjs controller的两种写法

时间:2015-07-27 16:57:58      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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

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


angularjs controller的两种写法

标签:

原文地址:http://my.oschina.net/zilinangel1986/blog/484274

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!