AngularJS Tutorial - Building A Web App in 5 Minutes PDF
AngularJS Tutorial - Building A Web App in 5 Minutes PDF
(/)
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
LEARN (/LEARN) > POSTS (/POSTS) > ANGULARJS () > NICK KAYE
(/about)
Nick has more than 20 years of experience building web applications for
companies like Chase, Nintendo, and General Electric.
Table of Contents
1 Introduction
2 Is it easy to build my own web app?
3 AngularJS Tutorial: Business
Website
4 Best Practices: Why Do We Care?
4.1 Good Code vs. Bad Code
4.2 AngularJS is a Great Solution
4.3 Better App Development
4.4 Blank Canvas
5 What exactly is AngularJS?
6 AngularJS is Easy to Implement
7 Excellent architecture with
AngularJS
1 Introduction
Whats the essential advantage of Angular? Whether youre a beginner building your
own website, or a professional seeking an edge on how to learn better app building,
this AngularJS tutorial is a great investment of 5 minutes to learn the best practices of
creating apps from a veteran software engineer and AngularJS expert.
DOWNLOAD: The Github repository of this AngularJS web app is available here
(https://github.com/airpair/T0021-airpair-angularjs-tutorial).
1 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
The Internet, Super-hardcore geeks are working together to solve all of the common
problems of web development by building modular, reusable solutions. These open
source projects put simple, powerful tools in the nimble hands of creative people.
Powerful tools like AngularJS.
To skip over the construction of a basic static website, well implement a free website
theme (https://wrapbootstrap.com) based on Bootstrap (http://getbootstrap.com/). For
this example we'll use, http://startbootstrap.com/modern-business
(http://startbootstrap.com/modern-business). We download the website template and
unzip it.
The original le structure is as one might expect in such a website template. We have a
css folder, js folder, and a whole lot of html les.
2 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
(http://airpair-blog.s3.amazonaws.com/wp-content/uploads/2014/07/websitetemplate-legacy.png)
Theres the Navigation (at the top), and the Home Page of the site (all the content in the
middle). Everything looks ne.. right?
3 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
(/about)
index.html
<!DOCTYPE html>
<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
markup
Notice (Our Website Content Goes Here) weve gutted the inside of the index.html ,
so all we are left with is a best-practice blank canvas.
(http://airpair-blog.s3.amazonaws.com/wp-content/uploads/2014/07/empty-
4 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
new-index.png) Fear not! All is well. This blank canvas is the foundation of our ability to
separate our site wide concerns such as browser compliance and linking vendor
dependencies. All of those things are functioning correctly in the background, thanks
to our new index.html.
Google describes Angular as what HTML would have been, had it been designed for
building web-apps. Bearing that in mind, lets take another look at our index.html, this
time using Firefox Developer Tools Firebug 3D View (https://developer.mozilla.org
/en-US/docs/Tools/3D_View):
(http://airpair-blog.s3.amazonaws.com/wp-content/uploads/2014/07/3d-viewof-template-page.png)
We see that HTML is already capable of describing a very organized Document Object
Model (DOM). The essential point of Angular is that we can harness the existing model
to create interactive experiences, instead of cluttering the code with new paradigms.
Interaction is driven by real people through their screens. Angular brings the DOM to
life; our page will be born interactive, without the clutter of having to write computer
code for every single aspect of things (e.g. loading sub-page templates, or populating
lists of items from data) that are actually relatively easy to say in plain language. When
Angular loads (and this is why we placed our 1 line of code at the end of the ) it will
look at the page and bring it to life based on extra information we have placed inside
of our HTML tags.
5 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
We add this 1 line just before the end of the of our index.html:
markup
<html>
<body>
<span>this app is so cool</span>
<p>its straight up magic</p>
<span class="highlight"><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js
</body>
</html>
(/about)
markup
Highlighted above, were dreaming really big with our URLs. Using a very powerful
aspect of Angular called Routes, well be able to bring those hashes to life as well!
6 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
Lets start with the About page. Well cut out just the content from the original .html le
from the free website theme. Then create a new le, partials/about.html and paste
only the unique content for the About page into this new .html le.
Heres the nal partials/about.html:
<div class="container">
markup
<div class="row">
(/about)
<div class="col-lg-12">
<span class="page-header">About
<small>It's Nice to Meet You!</small>
</span>
</div>
</div>
<!-- etc., more information about the company. -->
</div>
Get expert markup help (/find-an-expert?utm_source=airpair.com&utm_campaign=posts-{{campPeriod}}&utm_content={{vie
7 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
<!DOCTYPE html>
<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js"> <!--<![endif]-->
markup
<head>
(/about)
<!-- Meta-Information -->
<!-- etc -->
<!-- Vendor: Bootstrap Stylesheets http://getbootstrap.com -->
<!-- etc -->
<!-- Our Website CSS Styles -->
<link rel="stylesheet" href="css/main.css">
Get expert markup help (/find-an-expert?utm_source=airpair.com&utm_campaign=posts-{{campPeriod}}&utm_content={{vie
Instead of cluttering up our index.html with a lot of content thats proprietary to the
individual sub-pages, weve used Angular Directives isolated it to a stack of side-wide
horizontal layers of functionality.
The ng-app directive binds the Angular app to this single pages html body.
<body <span class="highlight">ng-app="tutorialWebApp"</span>>
markup
The ng-include directive eortlessly performs all of the heavy lifting required to load
our isolated site wide template le templates/header.html.
<div <span class="highlight">ng-include='"templates/header.html"'</span>></div>
markup
Awesomely, the ng-view directive provides an automatic container, into which Angular
Routes will seamlessly include our page content templates/*.html
<div <span class="highlight">ng-view</span>></div>
markup
8 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
/**
* Main AngularJS Web Application
*/
var app = angular.module('tutorialWebApp', [
'ngRoute'
]);
markup
/**
* Configure the Routes
*/
app.config(['$routeProvider', function ($routeProvider) {
(/about)
$routeProvider
// Home
.when("/", {templateUrl: "partials/home.html", <span class="highlight">controller: "PageCtrl"
// Pages
.when("/about", {templateUrl: "partials/about.html", <span class="highlight">controller: "PageCtrl"
.when("/faq", {templateUrl: "partials/faq.html", <span class="highlight">controller: "PageCtrl"
/* etc routes to other pages */
// Blog
Get expert markup help (/find-an-expert?utm_source=airpair.com&utm_campaign=posts-{{campPeriod}}&utm_content={{vie
See the highlighted sections above? Weve touched on the nal concept of this tutorial,
Angular Controllers. In the next and nal step, well create each of these Controllers
weve described in the route conguration above.
markup
/**
* Controls all other Pages
*/
app.controller('PageCtrl', function (/* $scope, $location, $http */) {
console.log("Page Controller reporting for duty.");
// Activates the Carousel
$('.carousel').carousel({
interval: 5000
});
// Activates Tooltips for Social Links
Get expert markup help (/find-an-expert?utm_source=airpair.com&utm_campaign=posts-{{campPeriod}}&utm_content={{vie
9 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
(/about)
(http://airpair-blog.s3.amazonaws.com/wp-content/uploads/2014/07
/4xScreenshots.png)
View the nal working demo:
http://airpair.github.io/demos/2014/09/T0021-
airpair-angularjs-tutorial (http://airpair.github.io/demos/2014/09/T0021-airpairangularjs-tutorial)
Final source code is available on GitHub:
https://github.com/airpair/T0021-airpair-
angularjs-tutorial (https://github.com/airpair/T0021-airpair-angularjs-tutorial)
10 of 11
12/23/2014 7:34 PM
https://www.airpair.com/angularjs/building-angularjs-app-tutorial#7-4-us...
(/about)
angularjs (/posts/tag/angularjs)
javascript (/posts/tag/javascript)
Similar posts
(https://www.airpair.com AngularJS: From
/angularjs/posts/angularjsPrototyping to
from-prototypingFunctional Code
to-functional-code
How to scale a simple
angularJS prototype into
angularjs
a shippable and
maintainable product
Fernando
Villalobos
06 Dec, 2014
11 of 11
Daniel
Lamb
28 Nov, 2014
jQuery vs.
AngularJS: A
Comparison and
Migration
Walkthrough
JavaScript expert Daniel
Lamb contrasts
AngularJS with jQuery
and shows how to
(https://www.airpair.com AngularJS
/angularjs/posts/angularjsPerformance in
performance-largeLarge Applications
applications
AngularJS expert
Abraham Polishchuk
angularjs
walks through
javascript
strategies to maximize
performance in large
Angular apps.
abraham
polishchuk
27 Nov, 2014
12/23/2014 7:34 PM