AngulatJS factory 使用Module(模块)组织依赖关系
2014-04-13 17:37
417 查看
1 使用Module(模块)组织依赖关系
<!DOCTYPE html> <html ng-app="shoppingModule"> <head> <title></title> <script src="angular.min.js" type="text/javascript"></script> <script> var shoppingModule = angular.module("shoppingModule", []); shoppingModule.factory("Items", function () { var items = {}; items.query = function () { //在服务器中拉取数据 return [ { name: 'Jackey', age: 25 }, { name: 'Cassi', age: 20 }, { name: 'JC', age: 1.2 } ]; }; return items; }); shoppingModule.controller("shoppingController", function ($scope, Items) { $scope.Items = Items.query(); }); </script> </head> <body> <div ng-controller="shoppingController"> <ul> <li ng-repeat="item in Items"> {{item.name}} </li> </ul> </div> </body> </html>
需要注意的点是
1 controller里面
$scope.Items = Items.query();
2 factory里面的items.query = function(){};
2 添加过滤器
<!DOCTYPE html> <html ng-app="shoppingModule"> <head> <title></title> <script src="angular.min.js" type="text/javascript"></script> <script> var shoppingModule = angular.module("shoppingModule", []); shoppingModule.factory("Items", function () { var items = {}; items.query = function () { return [ { name: 'Jackey', age: 25 }, { name: 'Cassi', age: 20 }, { name: 'uuuuujC', age: 1.2 } ]; }; return items; }); //过滤器 shoppingModule.filter("titleCase", function () { var titleCase = function (input) { return input.charAt(0).toUpperCase() + input.slice(1); }; return titleCase; }); shoppingModule.controller("shoppingController", function ($scope, Items) { $scope.Items = Items.query(); }); </script> </head> <body> <div ng-controller="shoppingController"> <ul> <li ng-repeat="item in Items"> {{item.name | titleCase}} </li> </ul> </div> </body> </html>
相关文章推荐
- javascript
- jsp网页产生验证码
- JavaScript详解
- JavaScript DOM-Ready 机制
- 包含多集合的json反序列化
- JS事件
- javascript通过url向jsp页面传递中文参数乱码解决方法
- javaScript-console对象
- href="#"与href="javascript:void(0)"的区别
- Jsp与Servlet之间的传值
- JavaScript中冒号(:)
- js-DOM[1] 绑定~
- JSP页面间传递参数
- ExtJS 4.0 的改变--较为完整的介绍
- 深入理解JavaScript系列(47):对象创建模式(上篇)
- html5学习canvas基本使用:div在js前面;localStorage存储根据浏览器在不同的位置;应用程序缓存;placeholder 属性提供一种提示(hint),描述输入域所期待的值。
- javascript学习之类型检测
- JSTL+EL构建购物车
- 使用pjsip中的库
- jsp页面实现按时间段查询记录和快速查询