Notice:
This is just my personal AngularJS refresher. In other words these are some of my notes while taking the AngularJS course at codeschool.com.
So not all of this is my stuff but it can help you as a refresher too, if you are looking for a great intro to AngularJS I highly encourage you to follow the course itself at codeschool.com.
Some theory:
Directives: HTML annotations that trigger js behavior. Many useful directives are provided by angularJS.
Modules: Where app components live. Help to organize and encapsulate code.
Controllers: Where we define app behavior by defining functions and values.
Expressions: {{ … }} stuff.
Angular claims to be MVW:
Model (ClientSide Model shared by Model and Whatever)
View (Where we use HTML, Directives, Modules, Expressions)
Whatever (Where we define Controllers, Modules, Directives)
Directives and “whatever”
Directive: ng-app - Attach the application module to the page.
1
<htmlng-app="gemStore">
Related JS (Whatever):
1
varapp=angular.module('gemStore',[]);
Directive: ng-controller - Attach a controller function to the body element (since it is inside the
tag, the scope is within the . For example it could also be a div).
1
<bodyng-controller="StoreController as store">
Related JS (Whatever):
1
2
3
app.controller('StoreController',function(){this.products=gems;//Put data into a “model”
});
Note: A controller is attached to an app.
**Directive**: `ng-repeat` - Loop, repeats the element for each item in the array.
1
2
3
<divclass="product row"ng-repeat="product in store.products"><h3>{{product.price}}</h3></div>
# Angular Filters
Use a pipe ‘|’ to say “output ‘product.price’ into the ‘currency’ filter”.
1
2
3
4
5
6
7
8
{{product.price|currency}}// Currency
{{'1388123412323'|date:'MM/dd/yyyy @ h:mma'}}// Date
this.review.createdOn=Date.now();// If we use this in controller
{{review.createdOn|date}}// Date as Aug 3, 2014
{{'anyString'|uppercase}}// Uppercase, lowercase
{{'My Description'|limitTo:8}}// Number of characters
<ling-repeat="product in store.products | limitTo:3">//Limit iterations
<ling-repeat="product in store.products | orderBy:'-price'">// Order by, minus(-) means desc, none means asc.
# Working with images
1
2
3
4
5
<imgng-src="{{product.images[0].full }}"/><ling-repeat="image in product.images">// Iterate/Repeat over an array of images.
<divng-show="product.images.length">// Show only if the array is not empty.
// A new controller...
...app.controller('TabController',function(){this.tab=1;this.setTab=function(setTabValue){this.tab=setTabValue;};this.isSet=function(tabValue){returnthis.tab==tabValue;};});...// Respective directives in html code
...<sectionng-controller="TabController as tab"><ul><ling-class="{active: tab.isSet(1)}"><ahrefng-click="tab.setTab(1)">Description</a></li><divng-show="tab.isSet(2)"><h4>Specs</h4><blockquote>{{product.shine}}</blockquote></div>...
// Now for setting a default
...app.controller('GalleryController',function(){this.current=0;this.setCurrent=function(newGallery){this.current=newGallery||0;};});...// Respective directives
// Note we use properties from different controllers
...<bodyng-controller="StoreController as store"><divng-repeat="product in store.products"><h3>{{product.name}}({{product.price|currency}})</h3>// Image Gallery
<divclass='gallery'ng-show="product.images.length"ng-controller='GalleryController as gallery'><imgng-src="{{product.images[gallery.current] }}"/>...
// Adding a review to a list of reviews - new controller!
...app.controller('ReviewController',function(){this.review={};this.addReview=function(productReviewed){productReviewed.reviews.push(this.review);// Add an element to the array
this.review={};};});...// The directives and html related. Note the preview and its relation with ng-model. (double directional binding)
<formname="reviewForm"ng-controller="ReviewController as reviewCtrl"ng-submit="reviewCtrl.addReview(product)">...<strong>{{reviewCtrl.review.stars}}Stars</strong>...<selectng-model="reviewCtrl.review.stars"ng-options="stars for stars in [5,4,3,2,1]"title="Stars"><optionvalue="">RatetheProduct</option></select>...<inputtype="submit"value="Submit Review"/></form>
# Form Validations
1
2
3
4
5
6
7
8
// novalidate: Turns off default validation by some browsers
<formname=”reviewForm”novalidate>
// required: Sets the form element as required, handled and defined by angular. Can validate URLs, emails,
<inputname=”author”ng-model=”reviewCtrl.revire.author”type=”email”required/>
// evaluates to true or false on validations implemented by angular.
{{ reviewForm.$valid }}
1
2
3
4
// Styles used by Angular
class="ng-pristine ng-invalid" // style in code when form loads
class="ng-dirty ng-invalid" // style when typing and invalid
class="ng-dirty ng-valid" // style when typing and valid
// Submit form only if form is valid.
ng-submit="reviewForm.$valid && reviewCtrl.addReview(product)"
# Custom Directives
Why? Let you write “expressive” html, easier to read and to understand its behavior.
We use: `ng-include`:
1
2
3
4
5
6
7
// In the html file
<divng-show="tab.isSet(1)"ng-include="'product-description.html'"></div>// In a separate file “product-description.html”:
<h4>Description</h4><blockquote>{{product.description}}</blockquote>
Example: Defining the directive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
app.directive("productDescription",function(){return{restrict:'E',// E=Element, A=Attribute
templateUrl:"product-description.html"};});// related html code when directive is of type "element":
<div><product-descriptionng-show="tab.isSet(1)"></product-description></div>// similar html code when directive is of type "attribute":
<divproduct-specsng-show="tab.isSet(2)">
...app.directive("productTabs",function(){return{restrict:'E',templateUrl:"product-tabs.html",controller:function(){this.tab=1;this.isSet=function(checkTab){returnthis.tab===checkTab;};this.setTab=function(setTab){this.tab=setTab;};},controllerAs:'tab'};});...// product-tabs.html:
...<ulclass="nav nav-pills"><ling-class="{ active:tab.isSet(1) }"><ahrefng-click="tab.setTab(1)">Description</a></li><ling-class="{ active:tab.isSet(2) }"><ahrefng-click="tab.setTab(2)">Specs</a></li></ul> ...// Now to use in index.html
<product-tabs></product-tabs>
# Modules and dependencies:
1
2
3
4
5
6
7
8
9
10
11
// Add store-directives as dependency of getStore
(function(){varapp=angular.module('gemStore',['store-directives']);->...})();(function(){varapp=angular.module('store-directives',[]);...})();
# Angular Built in services!
1
2
3
4
5
6
7
8
9
10
// Calling a REST Service
app.controller('StoreController',['$http',function($http){varstore=this;store.products=[];$http.get('/store-products.json').success(function(data){store.products=data;});}]);// It also provides logger, etc...
Final note not related to Angular: It has been really painful to write this post with Jekyll since the Angular {{ ... }} syntax requires especial handling to be displayed :s