先看看http://www.cnblogs.com/mbydzyr/p/3460501.html

http://www.oschina.net/translate/angularjs-factory-vs-service-vs-provider

使用factory创建服务

<!doctype html>
<html ng-app='ShoppingModule'> <head>
<meta charset="UTF-8">
<script src="angular.js"></script>
<script src="controller2.js"></script>
</head> <body ng-controller="ShoppingController">
<h1>Shop!</h1>
<table>
<tr ng-repeat='item in items'>
<td>{{item.title}}</td>
<td>{{item.description}}</td>
<td>{{item.price | currency}}</td>
</tr>
</table>
</div> </html>

JS

var shoppingModule = angular.module('ShoppingModule', []);
shoppingModule.factory('Items', function() {
var items = {};
items.query = function() {
// 在真实的应用中,我们会从服务端拉取这块数据 ...
console.log('service');
return [
{
title: 'Paint pots',
description: 'Pots full of paint',
price: 3.95
}, {
title: 'Polka dots',
description: 'Dots with polka',
price: 2.95
}, {
title: 'Pebbles',
description: 'Just little rocks',
price: 6.95
}
];
};
return items;
});
function ShoppingController($scope, Items) {
//console.log(Items.query());
$scope.items = Items.query();
}

最新文章

  1. Qt-导入第三方库
  2. input checkbox属性-Indeterminate状态
  3. 前端easyui的简化调用
  4. SAM4E单片机之旅——18、通过AFEC(ADC)获取输入的电压
  5. PS将图标变灰
  6. WPF常用数据绑定控件集合
  7. RIAidea – Focus on Flash/Flex/AIR » About Me
  8. Ubuntu通过使用PyCharm 进行调试 Odoo 8.0 可能出现的问题
  9. armstrong&#39;s programming erlang 2nd
  10. JS 弹出层 定位至屏幕居中
  11. React Native 系列(七) -- ListView
  12. POI处理Excel中的日期数据类型
  13. lua代码的加载
  14. scala for spark
  15. 16-使用Selenium模拟浏览器抓取淘宝商品美食信息
  16. phpcms栏目点击选中
  17. Objective-C Collection was mutated while being enumerated crash
  18. SVN报错:Node remains in conflict显示冲突的解决办法
  19. struts2 ognl存放数据
  20. 【bzoj4372】烁烁的游戏 动态点分治+线段树

热门文章

  1. openvswitch安装、基本操作
  2. HOJ1014
  3. Struts2知识总结
  4. Android安装应用失败UID 和 PID
  5. symfony2 关于是否需要建立多bundle
  6. 富文本编辑器ckeditor继承
  7. MySQL zerofill 的用法
  8. tomcat部署不正确
  9. .NET(C#):使用XPath查询带有命名空间(有xmlns)的XML
  10. 虚拟机的静态内部 IP 地址