Skip to content

Commit f2037de

Browse files
committed
demonstrates module injection
1 parent 267e860 commit f2037de

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

00-2-concepts.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script>
99
<script>
1010
//here we state that 'finance' is a dependancy
11-
angular.module('invoice-srv-demo', ['finance'])
11+
angular.module('invoice-srv-demo', ['finances'])
1212
//here we create a 'currencyConverter' and pass
1313
//it into the constructor for InvoiceController
1414
.controller('InvoiceController', ['currencyConverter', function(currencyConverter){

05-0-modules.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ Hello Jane Doe! You are from Canada`. This demonstrates the point.
230230
* Broadcasting and Emmiting
231231
`$emit(name, args)` will allow an event to be executed in current and parent scopes. `$broadcast(name,args)` will allow an event to be executed in current and child scopes.
232232
This will be demonstrated in future pages but can be seen very nicely on this page [Scope Events Propagation](https://docs.angularjs.org/guide/scope#scope-events-propagation).
233-
* `Controller as` Syntax refers to the following:
233+
* `Controller as` Syntax allows us to refer to the Controller by an *alias*:
234234
```html
235235
<script> function MyLongCtrlName(){
236236
this.clickMe = function(){
@@ -275,3 +275,4 @@ myAppComponents.directives.mydirective = function(){return {/*stuff here*/}};
275275
app.directive(myAppComponents.directives);
276276
app.controller(myAppComponents.controllers);
277277
```
278+
* When code bases become very large, we need modules. We have already seen that we have our main app in a module but we can actually create modules seperately. We have already shown this in [00-2-concepts.html](https://github.com/zafarali/learning-angular/blob/master/00-2-concepts.html) but just to drive home the point we reiterate using a directive and a factory in [05-0-modules.html](https://github.com/zafarali/learning-angular/blob/master/05-0-modules.html).

0 commit comments

Comments
 (0)