angular中,controller、directive和factory

1.angularjs的内置服务多,例如:$scope,$rootScope,$http,$q,$resource,$routeProvider等等。

2.Controller 干的事情仅仅就是把相应的数据或者方法绑定到 ViewModel 中。
随着 Angular 1.2 中 Controller As 语法的到来,就不再需要 $scope 了。

比如:

// Angular 1.x with 'controller as' in ES5

myApp.controller('myController', ['itemService', function(itemService) {
  this.currentItem = itemService.current();
  this.items = itemService.all();
  this.add = function(item) {
    itemService.add(item);
  };
}]);
把不变性的内容(比如基本上所有的方法)作为原型的属性而非对象的属性:
// Angular 1.x with 'controller as' in ES5
myApp.controller('myController', ['itemService', ItemController]);
function ItemController(itemService) {
  this.currentItem = itemService.current();
  this.items = itemService.all();
}
ItemController.protoype.add = function(item) {
    itemService.add(item);
};
4. Provider、Factory、Service、Value、Constant 
The Provider(不是泛指)的话具有可配置性,可以在程序运行前进行相应的设定;
Factory 和 Service 除了写法上没有什么区别,喜欢用什么用什么,不一定要按它的名字来;
Value 和 Constant 为单个对象依赖,Constant 可以在 Provider 的配置阶段使用,其他都不能。



作者:Trotyl Yu
链接:http://www.zhihu.com/question/27836513/answer/76686560
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值