3. What you need to know
Must know Not so important
Nice to know
Automated Testing
BDD - Behavior Driven Development
TDD - Test Driven Development
etc
HTML & CSS
JavaScript
jQuery
Ruby on Rails
Python, PHP, etc
Databases
4. Why Angular?
If youâre using JavaScript to create a dynamic website,
Angular is a good choice.
âą Angular helps you organize your JavaScript
âą Angular helps create responsive (as in fast) websites.
âą Angular plays well with jQuery
âą Angular is easy to test
6. Web Server
HTML JavaScript
Response with Webpage & Assets
Web Browser
URL Request to server
Response with Webpage & Assets
User clicks on link, new Request HTML JavaScript
Browser loads up
entire webpage.
Browser loads up
entire webpage.
8. Web Server
Response with Webpage & Assets
Web Browser
URL Request to server
Response with JSON Data
User clicks on link, new Request
HTML JavaScript
DATA
Browser loads up
entire webpage.
Data is loaded into
existing page.
10. A client-side JavaScript Framework for adding interactivity to HTML.
app.js
index.html
How do we tell our HTML when to trigger our JavaScript?
<!DOCTYPE html>âš
<html>âš
<bodyâš
. . .
</body>âš
</html>
function Storeâš
alert('Welcome, Gregg!');âš
}âš
(){
>
What is Angular JS?
11. <!DOCTYPE html>âš
<html>âš
<bodyâš
. . .
</body>âš
</html>
app.js
function Storeâš
alert('Welcome, Gregg!');âš
}âš
A Directive is a marker on a HTML tag that tells Angular to run or reference
some JavaScript code.
Directives
Name of
function to call
index.html
Controller(){
ng-controller="StoreController">
15. Modules
âą Where we write pieces of our Angular application.
âą Makes our code more maintainable, testable, and readable.
âą Where we define dependencies for our app.
Modules can use other Modules...
16. Creating Our First Module
Dependencies
Other libraries we might need. âš
We have none... for nowâŠ
Application
Name
AngularJS
var app = angular.module('store', [ ]);
18. Including Our Module
var app = angular.module('store', [ ]); app.js
<!DOCTYPE html>âš
<html ng-app="store">âš
<head>âš
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />âš
</head>âš
<body>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body>âš
</html>
Run this module
when the document
loads.
index.html
19. Allow you to insert dynamic values into your HTML.
Expressions
Numerical Operations
evaluates to
String Operations
evaluates to
+ More Operations:
http://docs.angularjs.org/guide/expression
<p>âš
I am {{4 + 6}}âš
</p>
<p>âš
{{"hello" + " you"}}âš
</p>
<p>âš
I am 10âš
</p>
<p>âš
hello youâš
</p>âš
22. Working With Data
...just a simple
object we want to
print to the page.
var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',âš
}.
23. Controllers
app.js
Notice that controller is attached to (inside) our app.
Controllers are where we define our appâs behavior by defining
functions and values.
Wrapping your Javascript
in a closure is a good habit!
var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',âš
}.
})();
(function(){âš
var app = angular.module('store', [ ]);
app.controller('StoreController', function(){âš
âš
});
24. Storing Data Inside the Controller
app.js
Now how do we
print out this
data inside our
webpage?
this.product = gem;
var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',âš
}.
})();
(function(){âš
var app = angular.module('store', [ ]);
app.controller('StoreController', function(){âš
âš
});
25. <!DOCTYPE html>âš
<html ng-app="store">âš
<head>âš
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />âš
</head>âš
!
!
!
!
!
!
!
!
</html>âš
<body>âš
<div>âš
<h1> Product Name </h1>âš
<h2> $Product Price </h2>âš
<p> Product Description </p>âš
</div>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body>
Our Current HTML
Letâs load our data into
this part of the page.
}
index.html
29. Understanding Scope
Would never print a
value!
The scope of
the Controller
is only inside
here...
}
{{store.product.name}}
<h1> {{store.product.name}} </h1>âš
<h2> ${{store.product.price}} </h2>âš
<p> {{store.product.description}} </p>
<body>âš
<div ng-controller="StoreController as store">âš
!
!
</div>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body>
32. var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',
}.
canPurchase: false
</div>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body>
Adding A Button
index.html
<button
How can we
only show
this button...
...when this is true?
Directives to
the rescue!
<body ng-controller="StoreController as store">âš
<div>âš
<h1> {{store.product.name}} </h1>âš
<h2> ${{store.product.price}} </h2>âš
<p> {{store.product.description}} </p>
> Add to Cart </button>
33. var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',
}.
canPurchase: false
</div>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body>
NgShow Directive
index.html
<button
<body ng-controller="StoreController as store">âš
<div>âš
<h1> {{store.product.name}} </h1>âš
<h2> ${{store.product.price}} </h2>âš
<p> {{store.product.description}} </p>
ng-show="store.product.canPurchase"> Add to Cart </button>
Will only show the element if the
value of the Expression is
true.
34.
35. </div>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body> index.html
<button
<body ng-controller="StoreController as store">âš
<divâš
<h1> {{store.product.name}} </h1>âš
<h2> ${{store.product.price}} </h2>âš
<p> {{store.product.description}} </p>
ng-show="store.product.canPurchase"> Add to Cart </button>
NgHide Directive
If the product is sold out
we want to hide it.
var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',
}.
canPurchase: true,
soldOut: true
>
36. </div>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body> index.html
<button
<body ng-controller="StoreController as store">âš
<divâš
<h1> {{store.product.name}} </h1>âš
<h2> ${{store.product.price}} </h2>âš
<p> {{store.product.description}} </p>
ng-show="store.product.canPurchase"> Add to Cart </button>
NgHide Directive
If the product is sold out
we want to hide it.
This is awkward and
a good example to
use ng-hide!
var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',
}.
canPurchase: true,
soldOut: true,
ng-show="!store.product.soldOut">
37. </div>âš
<script type="text/javascript" src="angular.min.js"></script>âš
<script type="text/javascript" src="app.js"></script>âš
</body> index.html
<button
<body ng-controller="StoreController as store">âš
<divâš
<h1> {{store.product.name}} </h1>âš
<h2> ${{store.product.price}} </h2>âš
<p> {{store.product.description}} </p>
ng-show="store.product.canPurchase"> Add to Cart </button>
NgHide Directive
If the product is sold out
we want to hide it.
Much better!
var gem = {âš
name: 'Dodecahedron',âš
price: 2.95,âš
description: '. . .',
}.
canPurchase: true,
soldOut: true,
ng-hide="store.product.soldOut">
39. Multiple Products
app.js
var gems = [âš
âš
name: "Dodecahedron",âš
price: 2.95,âš
description: ". . .",âš
canPurchase: true,âš
},âš
{âš
name: "Pentagonal Gem",âš
price: 5.95,âš
description: ". . .",âš
canPurchase: false,âš
}âŠâš
];
{
Now we have an array...
How might we display all these
products in our template?
Maybe a
Directive?
app.controller('StoreController', function(){âš
this.products = gemsâš
});âš
;
So we have multiple products...
40. Working with An Array
index.html
<body ng-controller="StoreController as store">âš
<div>âš
<h1> {{store.products[0].name}} </h1>âš
<h2> ${{store.products[0].price}} </h2>âš
<p> {{store.products[0].description}} </p>âš
<button ng-show="store.products[0].canPurchase">âš
Add to Cart</button>âš
</div>âš
. . .âš
</body>
41. index.html
Working with An Array
. . .âš
</body>
Displaying the first product
is easy enough...
<body ng-controller="StoreController as store">âš
<div>âš
<h1> {{store.products[0].name}} </h1>âš
<h2> ${{store.products[0].price}} </h2>âš
<p> {{store.products[0].description}} </p>âš
<button ng-show="âš
Add to Cart</button>âš
</div>
store.products[0].canPurchase">
42. Working with An Array
index.html
<body ng-controller="StoreController as store">âš
<divâš
<h1> {{store.products[0].name}} </h1>âš
<h2> ${{store.products[0].price}} </h2>âš
<p> {{store.products[0].description}} </p>âš
<button ng-show="âš
Add to Cart</button>âš
</div>
. . .âš
</body>
<div>âš
<h1> {{store.products[1].name}} </h1>âš
<h2> ${{store.products[1].price}} </h2>âš
<p> {{store.products[1].description}} </p>âš
<button ng-show="store.products[1].canPurchase">âš
Add to Cart</button>âš
</div>
Why repeat yourself?
Why repeat yourself?
Why... You get it.
>
store.products[0].canPurchase">
That works...
43. Working with An Array
index.html
<body ng-controller="StoreController as store">âš
<divâš
<h1> {{product.name}} </h1>âš
<h2> ${{product.price}} </h2>âš
<p> {{product.description}} </p>âš
<button ng-show="âš
Add to Cart</button>âš
</div>
. . .âš
</body>
ng-repeat="product in store.products"
product
>
Repeat this
section for
each product.
}
.canPurchase">
44. What We Have Learned So Far
Directives â HTML annotations that trigger Javascript behaviors
Modules â Where our application components live
Controllers â Where we add application behavior
Expressions â How values get displayed within the page