Presented by Carlos Justiniano / @cjus / Presentation version: 0.5
Originally, Angular was designed as a way of enabling designers to use HTML markup to build application without having to know how to program
The core belief is that the best way to build user interfaces is by using declarative programming
In this approach, designers would be able to declare the behavior or components without knowing how the underlying behaviors were implemented
Here's the structure of the basic angular enabled page we'll reuse in this presentation.
<!DOCTYPE html>
<html ng-app>
<head>
<title>Basic Angular App</title>
</head>
<body>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
Before we dig too deeply into code lets examine a few core concepts...
Angular embraces HTML, by allowing designers and developers to describe what their HTML should do
The HTML5 data attribute allows designers to specify additional properties and behaviors for HTML5 elements.
By querying the DOM, programmers can detect the presence of attributes to augment the element.
In this example, jQueryMobile uses the data-role and data-filter-placeholder attributes to determine how the list should be transformed
<ul data-role="listview" data-filter="true"
data-filter-placeholder="Search fruits..."
data-inset="true">
<li><a href="#">Apple</a></li>
<li><a href="#">Banana</a></li>
<li><a href="#">Cherry</a></li>
<li><a href="#">Cranberry</a></li>
<li><a href="#">Grape</a></li>
<li><a href="#">Orange</a></li>
</ul>
From the above to this...
To this:
Angular calls these directives
Earlier, we saw examples of directives when we looked at HTML for our basic angular app
<!DOCTYPE html>
<html ng-app>
<head>
<title>Basic Angular App</title>
</head>
<body>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
The ng-app in the markup is an Angular directives used to inform Angular where the Angular application begins
Angular support application separation of concerns using MVC and modules such as services and routers
!doctype html>
<html ng-app>
<head>
</head>
<body>
<div>
<label>Name:</label>
<input type="text" ng-model="yourName"
placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!</h1>
</div>
<script src="js/angular.min.js"></script>
</body>
</html>
The ng-model in the <input> markup above is an Angular directive used to bind data an input control
Expressions can be evaluated inside of mustache tags
<!DOCTYPE html>
<html ng-app>
<head>
<title>DataBinding</title>
</head>
<body>
<h2>Live text: {{ data.message }}</h2>
<h2>{{ "Hello " + "World!" }}</h2>
<h2>{{ 2 + 4}}</h2>
<label>Message: </label>
<input type="text" ng-model="data.message">
<script src="angular.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html ng-app>
<head>
<title>Data binding and Filters</title>
</head>
<body>
<p>1) Cost: {{ 11.99 + (11.99 * 0.33) | currency }}</p>
<p>2) Data: {{ 1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z' }}</p>
<p>3) Lowercase: {{ "Buy Now!" | lowercase }}</p>
<p>4) Uppercase: {{ "Buy Now!" | uppercase }}</p>
<p>5) Here is some JSON data: {{ {'Boy':'Fred', 'Girl':'Jill'} | json }}</p>
<script src="angular.min.js"></script>
</body>
</html>
1) Cost: {{ 11.99 + (11.99 * 0.33) | currency }}
2) Data: {{ 1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z' }}
3) Lowercase: {{ "Buy Now!" | lowercase }}
4) Uppercase: {{ "Buy Now!" | uppercase }}
5) Here is some JSON data: {{ {'Boy':'Fred', 'Girl':'Jill'} | json }}
<!DOCTYPE html>
<html ng-app>
<head>
<title>DataBindingWithClasses</title>
<style>
.red { color: red; } .green { color: green; } .blue { color: blue; }
</style>
</head>
<body>
<h1 class="{{ data.classColor }}">Basic Angular App</h1>
<label>Title color: </label>
<input type="text" ng-model="data.classColor">
<script src="angular.min.js"></script>
</body>
</html>
Data Binding with Classes
<!DOCTYPE html>
<html ng-app ng-init="data.message='Hello, from your application scope.';">
<head>
<title>Simple Scope</title>
</head>
<body>
<h2>Scope says: {{ data.message }}</h2>
<script src="angular.min.js"></script>
</body>
</html>
With Angular we can define our own element as I did below with the my ph-panel directive:
<ph-panel title="This is the panel's title">
<p>This is the panel's body.</p>
</ph-panel>
Here is the live code using the ph-panel directive:
This is the panel's body.
A few extra lines for good measure
A few extra lines for good measure
A few extra lines for good measure
A few extra lines for good measure
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Simple Angular Module</title>
</head>
<body>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
angular.module('myApp', []);
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Simple Controller</title>
</head>
<body >
<div ng-controller="myFirstController">
{{data.message}}
</div>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
angular.module('myApp', [])
.controller('myFirstController', function($scope) {
$scope.data = {
message: 'Hello from a simple controller!'
};
});
var myApp = angular.module('myApp', []);
myApp.controller('myFirstController', function($scope) {
$scope.data = {
message: 'Hello from a simple controller!'
};
});
angular.module('myApp', [])
.controller('myFirstController', function($scope) {
$scope.data = {
message: 'Hello from a simple controller!'
};
});
<div ng-controller="myFirstController">
<p>FIRST CONTROLLER<br>
First controllers message: [ {{data.message}} ]<br>
Second controllers message: [ {{data2.message}} ]<br>
Third controllers message: [ {{data3.message}} ]</p>
<div ng-controller="mySecondController">
<hr>
<p>SECOND CONTROLLER<br>
First controllers message: [ {{data.message}} ]<br>
Second controllers message: [ {{data2.message}} ]<br>
Third controllers message: [ {{data3.message}} ]</p>
</div>
</div>
<div ng-controller="myThirdController">
<hr>
<p>THIRD CONTROLLER<br>
First controllers message: [ {{data.message}} ]<br>
Second controllers message: [ {{data2.message}} ]<br>
Third controllers message: [ {{data3.message}} ]</p>
</div>
angular.module('myApp', [])
.controller('myFirstController', function($scope) {
$scope.data = {
message: 'Hello from the first controller!'
};
})
.controller('mySecondController', function($scope) {
$scope.data2 = {
message: 'Hello from the second controller!'
};
})
.controller('myThirdController', function($scope) {
$scope.data3 = {
message: 'Hello from the third controller!'
};
});
angular.module('myCoolApp', []);
angular.module('myCoolApp', ['ngRoute']);
angular.module('myCoolApp', ['ngRoute']);
.config(function($routeProvider) {
'use strict';
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Simple Service and Factory</title>
</head>
<body >
<div ng-controller="myController">
<p>Get User from Service: {{ userFromService }}</p>
<p>Create new user from Factory: {{ createUserWithFactory('Anatoly', 'Karpov') }}</p>
<p>Create another new user from Factory: {{ createUserWithFactory('Magnus','Carlsen') }}</p>
</div>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
angular.module('myApp', [])
.controller('myController', ['$scope', 'FirstService', 'FirstFactory',
function($scope, myFirstService, myFirstFactory) {
$scope.userFromService = myFirstService.getUser();
$scope.createUserWithFactory = function(firstName, lastName) {
var user = myFirstFactory.createUser(firstName, lastName);
return JSON.stringify(user);
};
}])
.service('FirstService', function() {
var user = {
"firstName": "Garry",
"lastName": " Kasparov"
};
this.getUser = function() {
return JSON.stringify(user);
};
})
.factory('FirstFactory', function() {
return {
createUser: function(firstName, lastName) {
return {
"firstName": firstName,
"lastName": lastName
};
}
};
});
Get User from Service: {"firstName":"Garry","lastName":" Kasparov"}
Create new user from Factory: {"firstName":"Anatoly","lastName":"Karpov"}
Create another new user from Factory: {"firstName":"Magnus","lastName":"Carlsen"}
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Provider</title>
</head>
<body >
<div ng-controller="myController">
<p>Get start Date: {{ startDate }}</p>
</div>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
angular.module('myApp', [])
// controller
.controller('myController', ['$scope', 'StartDate',
function($scope, myDateProvider) {
$scope.startDate = myDateProvider.getStartDate();
}])
// provider
.provider('StartDate', function() {
this.startDate = new Date();
this.$get = function() {
var startDate = this.startDate;
return {
getStartDate: function() {
return startDate;
}
};
};
this.setStartDate = function(newDate) {
this.startDate = newDate;
};
})
// config
.config(function(StartDateProvider) {
//StartDateProvider.setStartDate(new Date("October 13, 1975 11:13:00"));
});
So this:
<input class="ipt" type="text" placeholder="name" require ng-model="user.name">
Rather than this:
<input require class="ipt" ng-model="user.name" placeholder="name">
$timeout instead of setTimeout
$interval instead of setInterval
$window instead of window
$document instead of document
$http instead of $.ajax
describe('Simple Test Spec', function() {
it('should just pass', function() {
expect(true).toBe(true);
});
});
~/AngularPresentation$ ./karma_start.sh
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 35.0.1916 (Mac OS X 10.9.3)]: Connected on socket Q47fUfs2QdHA5DNhAjXi with id 33541008
Chrome 35.0.1916 (Mac OS X 10.9.3): Executed 1 of 1 SUCCESS (0.01 secs / 0.008 secs)
describe('Simple Test Spec', function() {
it('should just pass', function() {
expect(true).toBe(false);
});
});
INFO [watcher]: Changed file "/Users/cjus/dev/cjus/AngularPresentation/samplecode/SimpleTest/simpleTestSpec.js".
Chrome 35.0.1916 (Mac OS X 10.9.3) Simple Test Spec should just pass FAILED
Expected true to be false.
Error: Expected true to be false.
at null. (/Users/cjus/dev/cjus/AngularPresentation/samplecode/SimpleTest/simpleTestSpec.js:3:18)
Chrome 35.0.1916 (Mac OS X 10.9.3): Executed 2 of 2 (1 FAILED) (0.032 secs / 0.028 secs)
describe('Angular element and expression test', function() {
var element
, $scope;
beforeEach(module("myApp"));
beforeEach(inject(function($compile, $rootScope) {
$scope = $rootScope;
element = angular.element("<div>{{2 + 2}}</div>");
element = $compile(element)($rootScope);
$scope.$digest();
}));
it('should equal 4', function() {
expect(element.html()).toBe("4");
});
});
describe('Angular controller test', function() {
var myController
, scope;
beforeEach(module("myApp"));
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
myController = $controller('myController', {
$scope: scope
});
}));
it('should have a getDate function which returns a date', function() {
expect(scope.getDate()).not.toBeNull();
});
});
describe('Angular service test', function() {
var Base64Service;
beforeEach(module("myApp"));
beforeEach(inject(function(_Base64Service_) {
Base64Service = _Base64Service_;
}));
it('should correctly encode a known message', function() {
expect(Base64Service.encode('Hello from a simple controller!')).toBe('SGVsbG8lMjBmcm9tJTIwYSUyMHNpbXBsZSUyMGNvbnRyb2xsZXIlMjE=');
});
});
Google members have written a number of excellent tools which support AngularJS development
TODO: Angular life cycle
2013, the Angular team hosted an internal conference at Google offices
Initially, 50 people were expected to attend
197 attendees showed up
The team announced that by mid-2013 the popularity of Angular skyrocketed in comparison to other frameworks
Named after the HTML angled bracket
You might think all of Angular's features result in a large and bloated framework