|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Modules</title> |
| 5 | + <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script> |
| 6 | + <script> |
| 7 | + //myApp depends on ShoppingModule |
| 8 | + var myApp = angular.module('myApp', ['ShoppingModule']); |
| 9 | + |
| 10 | + //here we inject the 'Items' factory into our controller |
| 11 | + myApp.controller('ShoppingController',['$scope','Items', function($scope,Items){ |
| 12 | + this.items = Items.query(); |
| 13 | + }]); |
| 14 | + |
| 15 | + |
| 16 | + //here our ShoppingModule contains only one factory and one directive. |
| 17 | + angular.module('ShoppingModule', []) |
| 18 | + .factory('Items', function(){ |
| 19 | + var query = function(){ |
| 20 | + //collect data from the server |
| 21 | + return [ |
| 22 | + {product:'Mobile Phone', price:2.50}, |
| 23 | + {product:'Laptop', price:5.00}, |
| 24 | + {product:'Book', price:7.50} |
| 25 | + ]; |
| 26 | + }; |
| 27 | + return {query:query}; |
| 28 | + }) |
| 29 | + .directive('product', function(){ |
| 30 | + return { |
| 31 | + restrict:'E', |
| 32 | + scope:{product:'@', price:'@'}, |
| 33 | + template:'<span>{{product}} costs {{price | currency}}</span>' |
| 34 | + } |
| 35 | + }) |
| 36 | + </script> |
| 37 | +</head> |
| 38 | +<body> |
| 39 | +<div ng-app="myApp" ng-controller='ShoppingController as shop'> |
| 40 | +<div ng-repeat='item in shop.items'> |
| 41 | + |
| 42 | +<!--This product directive comes from the Shopping Module--> |
| 43 | +<product product="{{item.product}}" price="{{item.price}}"></product> |
| 44 | +</div> |
| 45 | +</body> |
| 46 | +</html> |
0 commit comments